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 |
skillilea
Starting Member
15 Posts |
Posted - 2012-03-29 : 13:01:51
|
I am trying to create a return set with the monthly value and and average of the last 3 month rolling.Looks like this.Date MonValue 3MonthBackValueValues are stored like this.3/1/2011 104/1/2011 115/1/2011 86/1/2011 153/1/2011 124/1/2011 145/1/2011 126/1/2011 12SO the ressult would be:Date MonValue 3MonthBackValue3/1/2011 10 AVG(10, 11, 8)4/1/2011 11 AVG(11, 8, 15)...ANY WAY TO DO THIS IN A PIVOT OR A CTE.I am passing in a start date and going 12 months back for the graph to start with.Thanks tons. |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-03-29 : 13:22:08
|
[code]CREATE TABLE #t (myDate date, myValue int)GOINSERT INTO #t(myDate, myValue)SELECT '3/1/2011', 10 UNION ALLSELECT '4/1/2011', 11 UNION ALLSELECT '5/1/2011', 8 UNION ALLSELECT '6/1/2011', 15 UNION ALLSELECT '3/1/2011', 12 UNION ALLSELECT '4/1/2011', 14 UNION ALLSELECT '5/1/2011', 12 UNION ALLSELECT '6/1/2011', 12GOSELECT DISTINCT YEAR(l.myDate), MONTH(l.myDate), YEAR(DATEADD(mm,2,l.myDate)), MONTH(DATEADD(mm,2,l.myDate)), r.myValueFROM #t lJOIN #t rON DATEDIFF(mm,l.myDate,r.myDate) < 3ORDER BY 1,2[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2012-03-29 : 14:35:44
|
Do not use this in a live environment, it is just a sample.Steps are 1: order the table 2: group3: querydrop table #t,#t1,#t2CREATE TABLE #t (myDate datetime, myValue decimal)INSERT INTO #t(myDate, myValue)SELECT '3/1/2011', 10 UNION ALLSELECT '4/1/2011', 11 UNION ALLSELECT '5/1/2011', 8 UNION ALLSELECT '6/1/2011', 15 UNION ALLSELECT '3/1/2011', 12 UNION ALLSELECT '4/1/2011', 14 UNION ALLSELECT '5/1/2011', 12 UNION ALLSELECT '6/1/2011', 12select row_number() over (order by (select 1))as Rowid,* into #t1from #tselect row_number() over (partition by month(mydate) order by rowid) as rowidgroup,*into #t2from #t1select a.rowid,a.rowidgroup,a.mydate,avg(b.myvalue)--select *from #t2 across apply(select * from #t2 aa where aa.mydate between a.mydate and dateadd(month,2,aa.mydate) and aa.rowid <= a.rowid + 2 and aa.rowidgroup = a.rowidgroup)b --order by a.rowidgroup,a.mydategroup by a.rowid,a.rowidgroup,a.mydateorder by rowidgroup,mydate/*rowid rowidgroup mydate (No column name)1 1 2011-03-01 00:00:00.000 9.6666662 1 2011-04-01 00:00:00.000 11.3333333 1 2011-05-01 00:00:00.000 11.5000004 1 2011-06-01 00:00:00.000 15.0000005 2 2011-03-01 00:00:00.000 12.6666666 2 2011-04-01 00:00:00.000 12.6666667 2 2011-05-01 00:00:00.000 12.0000008 2 2011-06-01 00:00:00.000 12.000000*/ Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
|
|
|
|