Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-25 : 06:31:24
|
| Hi,Could you help me, please??!!I created one query with the year-month and the total product by year-month.I would like to know, how I can accumulate in the last 12 months the products by year-month. Today I have this: I need this:1 2008 01 200801 1 2008 02 2008027 2008 03 2008033 2008 04 2008044 2008 05 2008053 2008 06 200806 9 2008 07 2008072 2008 08 2008084 200809 2008095 200810 2008100 200811 2008113 200812 2008121 200901 2009012 200902 45 200902 sum between 200801/2009022 200903 46 200903 sum between 200802/2009033 200904 48 200904 sum between 200803/2009042 200905 43 200905 ...2 200906 42 200906 ...4 200907 42 200907 ...4 200908 43 200908 ...2 200909 36 200909 ...Thank you!Regards, |
|
|
winterh
Posting Yak Master
127 Posts |
Posted - 2009-09-25 : 06:46:23
|
| You can use ROLLUP?Or try SUBSTRING(Convert (VarChar(8), DateColumnHere, 112) 1, 6) as 'Year & Month'Or TrySelect DatePart(Format_of_Date_Field_Here, DateColumn)Group BY Whatever_you_need_it_grouped_likeOr if you want it cummulated then use this link.http://www.sqlteam.com/article/calculating-running-totalsSearch before you post xD[ /fail at query] |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-25 : 07:33:00
|
| Hi,I created the column that show me the year-month 12 months before.But If I used this in the select, it show me:year-month qt year-month-12200902 2 200801And I don't know how can I accumulate the qt between the months 200801 and 200901 in this row.If I create a sum(qt) where year-month-12 <= year-month group by year-monthit is Still not working. show me only:year-month qt 200902 2 and I want to see:year-month qt 200902 45 (1+7+3+4+3+9+2+4+5+3+1+2)Sorry about my question.I am starting to working with SQL Server recently.Thank you for all! |
 |
|
|
winterh
Posting Yak Master
127 Posts |
Posted - 2009-09-25 : 08:47:19
|
| So you want to be able to see the month 12 monthes before any date you enter? And then an acummulated total for the 12 monthes leading up to the month you enter?You looking for something Like:SELECT TOP 12(yearMonth), Quantity, Sum(Quantity)FROM Your_TableORDER BY yearMonthOr something Like:Declare @CumulativeTotal Table(YearPrior DateTime,Total int)Insert into @CumulativeTotal(YearPrior, Total)Select DateAdd(month, -12, YearMonth), SUM(Select TOP 12(Quantity) From Your_Table Order By YearMonth)From Your_TableOrder BY yearMonthQuery Blaa Blaa goes here 9you can work this bit out)Sorta thing???Table names and column names would of been nice lol. I am trying to guage what you are getting at.[ /fail at query] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-25 : 09:31:55
|
| IIUYC, select sum(col),cast(cast(year_month as varchar(6))+'01' as datetime) from your_tablegroup by cast(cast(year_month as varchar(6))+'01' as datetime) MadhivananFailing to plan is Planning to fail |
 |
|
|
winterh
Posting Yak Master
127 Posts |
Posted - 2009-09-25 : 09:55:20
|
| Madhivanan, he wanted the columns to show like this I thinkYearMonth Quantity YearMonth-12 12MonthTotal200801 83 200701 921200802 75 200702 943Which is why you need a temp table to hold dateadd(month, -12, YearMonth)You know what I am trying to do in my queries Madhivanan?[ /fail at query] |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 03:13:58
|
| Yes....is that what I want!!!How I am doing this???Thank you! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-29 : 03:35:31
|
are you using SQL 2005 / 2008 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 04:37:31
|
| SQL Server 2005. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-29 : 04:41:17
|
use CROSS APPLY. . . something like this . . .select *from yourtable t cross apply ( select sum(somecol) as total_somecol from yourtable x where x.somedate >= dateadd(month, -12, t.somedate) and x.somedate <= t.somedate ) a KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 06:20:09
|
| I think that I did something wrong.Because it didn't work. Only duplicate each row 41 times.In my database, I don't have a Calendar key, I only have the stardate and leavedate in the table.Could you help me one more time, please?And sorry ....select qtd_sum, Calendar_Keyfrom[tmsdata].[tmsuser].[CE] as ttcross apply(select sum(qtd)as qtd_sum, Calendar_Keyfrom(select count (*) as qtd, Calendar_Keyfrom [tmsdata].[tmsuser].[CE],tmsuser.TMSEMP,(select [Year_Month] as Calendar_Keyfrom ( select '2007 01' as [Year_Month], '2007' as [Year Key]unionselect '2007 02' as [Year_Month], '2007' as [Year Key]unionselect '2007 03' as [Year_Month], '2007' as [Year Key]unionselect '2007 04' as [Year_Month], '2007' as [Year Key]unionselect '2007 05' as [Year_Month], '2007' as [Year Key]unionselect '2007 06' as [Year_Month], '2007' as [Year Key]unionselect '2007 07' as [Year_Month], '2007' as [Year Key]unionselect '2007 08' as [Year_Month], '2007' as [Year Key]unionselect '2007 09' as [Year_Month], '2007' as [Year Key]unionselect '2007 10' as [Year_Month], '2007' as [Year Key]unionselect '2007 11' as [Year_Month], '2007' as [Year Key]unionselect '2007 12' as [Year_Month], '2007' as [Year Key]unionselect '2008 01' as [Year_Month], '2008' as [Year Key]unionselect '2008 02' as [Year_Month], '2008' as [Year Key]unionselect '2008 03' as [Year_Month], '2008' as [Year Key]unionselect '2008 04' as [Year_Month], '2008' as [Year Key]unionselect '2008 05' as [Year_Month], '2008' as [Year Key]unionselect '2008 06' as [Year_Month], '2008' as [Year Key]unionselect '2008 07' as [Year_Month], '2008' as [Year Key]unionselect '2008 08' as [Year_Month], '2008' as [Year Key]unionselect '2008 09' as [Year_Month], '2008' as [Year Key]unionselect '2008 10' as [Year_Month], '2008' as [Year Key]unionselect '2008 11' as [Year_Month], '2008' as [Year Key]unionselect '2008 12' as [Year_Month], '2008' as [Year Key]unionselect '2009 01' as [Year_Month], '2009' as [Year Key]unionselect '2009 02' as [Year_Month], '2009' as [Year Key]unionselect '2009 03' as [Year_Month], '2009' as [Year Key]unionselect '2009 04' as [Year_Month], '2009' as [Year Key]unionselect '2009 05' as [Year_Month], '2009' as [Year Key]unionselect '2009 06' as [Year_Month], '2009' as [Year Key]unionselect '2009 07' as [Year_Month], '2009' as [Year Key]unionselect '2009 08' as [Year_Month], '2009' as [Year Key]unionselect '2009 09' as [Year_Month], '2009' as [Year Key]unionselect '2009 10' as [Year_Month], '2009' as [Year Key]unionselect '2009 11' as [Year_Month], '2009' as [Year Key]unionselect '2009 12' as [Year_Month], '2009' as [Year Key])as WorkDayCalendar )as tb1where CONVERT(char(4),DATEPART(year, tmsuser.CE.LEAVEDATE)) + ' ' +CASE WHEN DATEPART(month,tmsuser.CE.LEAVEDATE) < 10THEN ('0')ELSE ('')END+ CONVERT(char(2),DATEPART(month, tmsuser.CE.LEAVEDATE)) = Calendar_Keyand tmsuser.TMSEMP.empref = tmsuser.ce.emprefgroup by Calendar_Key)as tab2where Calendar_Key >= CONVERT(char(4),DATEPART(year, DATEADD(MONTH,-12, tt.LEAVEDATE))) + ' ' +CASE WHEN DATEPART(month,DATEADD(MONTH,-11, tt.LEAVEDATE)) < 10THEN ('0')ELSE ('')END+ CONVERT(char(2),DATEPART(month, DATEADD(MONTH,-11, tt.LEAVEDATE))) andCalendar_Key <= CONVERT(char(4),DATEPART(year, tt.LEAVEDATE)) + ' ' +CASE WHEN DATEPART(month,tt.LEAVEDATE) < 10THEN ('0')ELSE ('')END+ CONVERT(char(2),DATEPART(month, tt.LEAVEDATE))group by Calendar_Key)as tab_totalorder by 2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 06:26:04
|
| what do you want to show against each month? |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 06:34:45
|
| I only need to show the accumulation by month.In the example below I need to show: YearMonth and 12MonthTotal (200801 921 / 200802 943 ...)YearMonth Quantity YearMonth-12 12MonthTotal200801 83 200701 921200802 75 200702 943 |
 |
|
|
winterh
Posting Yak Master
127 Posts |
Posted - 2009-09-29 : 06:54:11
|
| http://www.sqlteam.com/article/calculating-running-totals use the link. I already posted it to you once.[ /fail at query] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 07:00:33
|
| what does 12 month total represent? total in 2007? |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 07:23:47
|
| If the month is 200901, represent the sum between 200801 and 200901.If the month is 200902, represent the sum between 200802 and 200902.If the month is 200903, represent the sum between 200803 and 200903.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 07:35:29
|
something like:-SELECT t.DisplayPeriod,t.TotalQty,t1.12MonthQtyFROM(SELECT DATENAME(yy,DATEADD(mm,DATEDIFF(mm,0,datefield,0)) + RIGHT('0'+ CAST(DATEPART(mm,DATEADD(mm,DATEDIFF(mm,0,GETDATE(datefield),0)) AS varchar(2)),2) AS DisplayPeriod,DATEADD(mm,DATEDIFF(mm,0,datefield,0) AS MonthDate,SUM(Qty) AS TotalQtyFROM YourTableGROUP BY DATEADD(mm,DATEDIFF(mm,0,datefield,0))tCROSS APPLY(SELECT SUM(Qty) AS 12MonthQty FROM YourTableWHERE datefield > = DATEADD(mm,-1,DATEADD(yy,-1,MonthDate))AND datefield <MonthDate)t1ORDER BY t.MonthDate |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2009-09-29 : 07:54:28
|
| Is this correct DATEDIFF(mm,0,datefield,0) ?Because I received the msg: The datediff function requires 3 argument(s)I chenged for that:DATENAME(yy,DATEADD(mm,DATEDIFF(mm,0,STARTDATE)) + RIGHT('0'+ CAST(DATEPART(mm,DATEADD(mm,DATEDIFF(mm,0,GETDATE(STARTDATE))) AS varchar(2)),2) AS DisplayPeriod,And now I received the msg:The dateadd function requires 3 argument(s)Thank you!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 07:58:58
|
| it should be DATEADD(mm,DATEDIFF(mm,0,datefield),0) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 08:04:48
|
i missed some bracesSELECT t.DisplayPeriod,t.TotalQty,t1.12MonthQtyFROM(SELECT DATENAME(yy,DATEADD(mm,DATEDIFF(mm,0,datefield),0)) + RIGHT('0'+ CAST(DATEPART(mm,DATEADD(mm,DATEDIFF(mm,0,datefield),0)) AS varchar(2)),2) AS DisplayPeriod,DATEADD(mm,DATEDIFF(mm,0,datefield),0) AS MonthDate,SUM(Qty) AS TotalQtyFROM YourTableGROUP BY DATEADD(mm,DATEDIFF(mm,0,datefield),0))tCROSS APPLY(SELECT SUM(Qty) AS 12MonthQty FROM YourTableWHERE datefield > = DATEADD(mm,-1,DATEADD(yy,-1,MonthDate))AND datefield <MonthDate)t1ORDER BY t.MonthDate |
 |
|
|
winterh
Posting Yak Master
127 Posts |
Posted - 2009-09-29 : 08:58:52
|
| Crazy[ /fail at query] |
 |
|
|
Next Page
|
|
|
|
|