| Author |
Topic |
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-24 : 10:53:54
|
| Hey all :)I need to calculate a rolling average per months.it means, I need to always calculate average for 6 months earlier.If I have this table:months X Rolling Average01-08 45 (45+0+0+0+0+0)/602-08 32 (32+45+0+0+0+0)/603-08 12 (12+32+45+0+0+0)/604-08 16 (16+12+32+45+0+0)/605-08 25 (25+16+12+32+45+0)/606-08 98 (98+25+16+12+32+45)/607-08 87 (87+98+25+16+12+32)/609-08 56 (56+87+98+25+16+12)/610-08 47 (47+56+87+98+25+16)/611-08 35 (35+47+56+87+98+25)/612-08 61 (61+35+47+56+87+98)/6Is there any built-in function who does that in Sql Server 2005?Please assistThank you! |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-24 : 11:10:15
|
| >>Is there any built-in function who does that in Sql Server 2005?no, but it is pretty simple to do your own.what is the datatype of your [months] column? Do you have multiple rows for each month or just one row per month in your base data?Be One with the OptimizerTG |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-24 : 11:17:26
|
| Hey TG :)Thanks for your reply!The months field is datetime.X is decimal.one row per month, exactly like in my example. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-24 : 11:19:02
|
| another question. Since the first row (10/08) has no earlier rows are you sure you want 45/6 rather than 45/1? Same for 02-08: (32+45)/2 etc.?Be One with the OptimizerTG |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-24 : 11:22:55
|
| Yes, it makes more sense, of course |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-24 : 11:23:50
|
Here is one way:set dateformat 'mdy'declare @t table (dt datetime, x int)insert @tselect '01/08/2008', 45 union allselect '02/08/2008', 32 union allselect '03/08/2008', 12 union allselect '04/08/2008', 16 union allselect '05/08/2008', 25 union allselect '06/08/2008', 98 union allselect '07/08/2008', 87 union allselect '09/08/2008', 56 union allselect '10/08/2008', 47 union allselect '11/08/2008', 35 union allselect '12/08/2008', 61 print 'This gives you a typical rolling avg based on available data'select t.dt ,t.x ,ca.ravgfrom @t tcross apply ( select avg(x) ravg from @t z where datediff(month, z.dt, t.dt) between 0 and 6 ) caprint 'This will give you your posted desired result'select t.dt ,t.x ,ca.ravgfrom @t tcross apply ( select sum(x)/6 ravg from @t z where datediff(month, z.dt, t.dt) between 0 and 6 ) caoutput:This gives you a typical rolling avg based on available datadt x ravg----------------------- ----------- -----------2008-01-08 00:00:00.000 45 452008-02-08 00:00:00.000 32 382008-03-08 00:00:00.000 12 292008-04-08 00:00:00.000 16 262008-05-08 00:00:00.000 25 262008-06-08 00:00:00.000 98 382008-07-08 00:00:00.000 87 452008-09-08 00:00:00.000 56 492008-10-08 00:00:00.000 47 542008-11-08 00:00:00.000 35 582008-12-08 00:00:00.000 61 64This will give you your posted desired resultdt x ravg----------------------- ----------- -----------2008-01-08 00:00:00.000 45 72008-02-08 00:00:00.000 32 122008-03-08 00:00:00.000 12 142008-04-08 00:00:00.000 16 172008-05-08 00:00:00.000 25 212008-06-08 00:00:00.000 98 382008-07-08 00:00:00.000 87 522008-09-08 00:00:00.000 56 492008-10-08 00:00:00.000 47 542008-11-08 00:00:00.000 35 582008-12-08 00:00:00.000 61 64 Be One with the OptimizerTG |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-24 : 11:40:44
|
| TG! many many thanks!I'm going to check this out.I will update you.Have a great day! :) |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-25 : 04:52:55
|
| Hey TG,It works partially..those are the dates i have:2008-06-01 00:00:00.000 works2008-07-01 00:00:00.000 works2008-08-01 00:00:00.000 works2008-09-01 00:00:00.000 works2008-10-01 00:00:00.000 works2008-11-01 00:00:00.000 works2008-12-01 00:00:00.000 DOESN'T work2009-01-01 00:00:00.000 DOESN'T workCould you please assist?Thank you so much! |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-03-25 : 05:31:23
|
Try thisset dateformat 'mdy'declare @t table (dt datetime, x decimal(10,2))insert @tselect '01/01/2008', 45 union allselect '02/01/2008', 32 union allselect '03/01/2008', 12 union allselect '04/01/2008', 16 union allselect '05/01/2008', 25 union allselect '06/01/2008', 98 union allselect '07/01/2008', 87 union allselect '09/01/2008', 56 union allselect '10/01/2008', 47 union allselect '11/01/2008', 35 union allselect '12/01/2008', 61SELECT A.DT, A.X, AVG(b.X)FROM @t A JOIN @t BON a.dt>=b.dt AND dateadd(month,-6,a.dt)<=b.dtGROUP BY A.DT, A.XORDER BY 1 Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-25 : 05:55:28
|
| Hey Mangal,It's not working.. i get the same results as before.I can give you my data to test on if you like?Thank you! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-03-25 : 05:56:02
|
[code]DECLARE @Sample TABLE ( theMonth DATETIME, theValue INT )INSERT @SampleSELECT '2008-01-01', 45 UNION ALLSELECT '2008-02-01', 32 UNION ALL SELECT '2008-03-01', 12 UNION ALLSELECT '2008-04-01', 16 UNION ALLSELECT '2008-05-01', 25 UNION ALLSELECT '2008-06-01', 98 UNION ALLSELECT '2008-07-01', 87 UNION ALLSELECT '2008-09-01', 56 UNION ALLSELECT '2008-10-01', 47 UNION ALLSELECT '2008-11-01', 35 UNION ALLSELECT '2008-12-01', 61SELECT s.theMonth, s.theValue, f.aFROM @Sample AS sCROSS APPLY ( SELECT AVG(1.0E * x.theValue) FROM @Sample AS x WHERE x.theMonth BETWEEN DATEADD(MONTH, -5, s.theMonth) AND s.theMonth ) AS f(a)ORDER BY s.theMonth[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-03-25 : 06:15:27
|
| Hi mayaaa,I added some rows, and it is calculating correctly.Yes please provide some sample data which is not giving correct result.Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
mayaaa
Starting Member
15 Posts |
Posted - 2009-03-25 : 06:29:35
|
| Hey Peso & Mangal :)thanks for the help, seems like Pesos solution is working great!! |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-03-25 : 07:17:35
|
| Oh i guess it is dateadd(month,-5,a.dt) and not dateadd(month,-6,a.dt).Mangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
|