Author |
Topic |
pcock
Starting Member
12 Posts |
Posted - 2009-01-08 : 00:04:12
|
Happy new year all!I'm not so sure how I can do this in TSQL. I'd like to find the moving average for last five transactions (for each customer) and write it to the 6th field on every row.customer_id purchase_date amount mov_avg1001 1/10/2008 18.8 1001 2/10/2008 227.07 1001 3/10/2008 202.9 1001 4/10/2008 218.94 1001 5/10/2008 92.8 1001 6/10/2008 378.33 152.101001 7/10/2008 351.87 224.011001 8/10/2008 437.01 248.971001 9/10/2008 169.64 295.791001 10/10/2008 411.2 285.931001 11/10/2008 423.61 349.612002 1/10/2008 410.17 2002 2/10/2008 37.8 2002 3/10/2008 244.86 2002 4/10/2008 244.1 2002 5/10/2008 227.01 2002 6/10/2008 169.95 232.792002 7/10/2008 432.32 184.742002 8/10/2008 457.42 263.652002 9/10/2008 495.79 306.162002 10/10/2008 65.95 356.502002 11/10/2008 313.36 324.29 Can someone please help me out? I'm using excel to do this and it's taking a whole lot of time :(Thanks |
|
DeveloperIQ
Yak Posting Veteran
71 Posts |
Posted - 2009-01-08 : 14:04:08
|
You can calculate the average for each row and in a second pass eliminate the results for all rows not multiples of 6. |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-08 : 15:50:44
|
Something like this ???select t.a,t.b,isnull(avg(t1.c) ,0)from (select '1001' a,convert(datetime,'1/10/2008',103) b, 18.8 c unionselect '1001', convert(datetime,'2/10/2008',103) , 227.07 unionselect '1001', convert(datetime,'3/10/2008',103), 202.9 unionselect '1001', convert(datetime,'4/10/2008',103), 218.94 unionselect '1001', convert(datetime,'5/10/2008',103), 92.8 unionselect '1001', convert(datetime,'6/10/2008',103), 378.33 unionselect '1001', convert(datetime,'7/10/2008',103), 351.87 unionselect '1001', convert(datetime,'8/10/2008',103), 437.01 unionselect '1001', convert(datetime,'9/10/2008',103), 169.64 unionselect '1001', convert(datetime,'10/10/2008',103), 411.2 unionselect '1001', convert(datetime,'11/10/2008',103), 423.61 unionselect '2002', convert(datetime,'1/10/2008',103), 410.17 unionselect '2002', convert(datetime,'2/10/2008',103), 37.8 unionselect '2002', convert(datetime,'3/10/2008',103), 244.86 unionselect '2002', convert(datetime,'4/10/2008',103), 244.1 unionselect '2002', convert(datetime,'5/10/2008',103), 227.01 unionselect '2002', convert(datetime,'6/10/2008',103), 169.95 unionselect '2002', convert(datetime,'7/10/2008',103), 432.32 unionselect '2002', convert(datetime,'8/10/2008',103), 457.42 unionselect '2002', convert(datetime,'9/10/2008',103), 495.79 unionselect '2002', convert(datetime,'10/10/2008',103), 65.95 unionselect '2002', convert(datetime,'11/10/2008',103), 313.36 )tleft join (select '1001' a,convert(datetime,'1/10/2008',103) b, 18.8 c unionselect '1001', convert(datetime,'2/10/2008',103) , 227.07 unionselect '1001', convert(datetime,'3/10/2008',103), 202.9 unionselect '1001', convert(datetime,'4/10/2008',103), 218.94 unionselect '1001', convert(datetime,'5/10/2008',103), 92.8 unionselect '1001', convert(datetime,'6/10/2008',103), 378.33 unionselect '1001', convert(datetime,'7/10/2008',103), 351.87 unionselect '1001', convert(datetime,'8/10/2008',103), 437.01 unionselect '1001', convert(datetime,'9/10/2008',103), 169.64 unionselect '1001', convert(datetime,'10/10/2008',103), 411.2 unionselect '1001', convert(datetime,'11/10/2008',103), 423.61 unionselect '2002', convert(datetime,'1/10/2008',103), 410.17 unionselect '2002', convert(datetime,'2/10/2008',103), 37.8 unionselect '2002', convert(datetime,'3/10/2008',103), 244.86 unionselect '2002', convert(datetime,'4/10/2008',103), 244.1 unionselect '2002', convert(datetime,'5/10/2008',103), 227.01 unionselect '2002', convert(datetime,'6/10/2008',103), 169.95 unionselect '2002', convert(datetime,'7/10/2008',103), 432.32 unionselect '2002', convert(datetime,'8/10/2008',103), 457.42 unionselect '2002', convert(datetime,'9/10/2008',103), 495.79 unionselect '2002', convert(datetime,'10/10/2008',103), 65.95 unionselect '2002', convert(datetime,'11/10/2008',103), 313.36 )t1on (t1.b=t.b-1 or t1.b=t.b-2 or t1.b=t.b-3 or t1.b=t.b-4 or t1.b=t.b-5 )and t.a=t1.agroup by t.a,t.border by 1,2 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-08 : 16:06:18
|
slightly modified, since you don't need averages for all rowsselect t.a,t.b,case when count(t1.a)<5 then null else isnull(avg(t1.c) ,0) endfrom (select '1001' a,convert(datetime,'1/10/2008',103) b, 18.8 c unionselect '1001', convert(datetime,'2/10/2008',103) , 227.07 unionselect '1001', convert(datetime,'3/10/2008',103), 202.9 unionselect '1001', convert(datetime,'4/10/2008',103), 218.94 unionselect '1001', convert(datetime,'5/10/2008',103), 92.8 unionselect '1001', convert(datetime,'6/10/2008',103), 378.33 unionselect '1001', convert(datetime,'7/10/2008',103), 351.87 unionselect '1001', convert(datetime,'8/10/2008',103), 437.01 unionselect '1001', convert(datetime,'9/10/2008',103), 169.64 unionselect '1001', convert(datetime,'10/10/2008',103), 411.2 unionselect '1001', convert(datetime,'11/10/2008',103), 423.61 unionselect '2002', convert(datetime,'1/10/2008',103), 410.17 unionselect '2002', convert(datetime,'2/10/2008',103), 37.8 unionselect '2002', convert(datetime,'3/10/2008',103), 244.86 unionselect '2002', convert(datetime,'4/10/2008',103), 244.1 unionselect '2002', convert(datetime,'5/10/2008',103), 227.01 unionselect '2002', convert(datetime,'6/10/2008',103), 169.95 unionselect '2002', convert(datetime,'7/10/2008',103), 432.32 unionselect '2002', convert(datetime,'8/10/2008',103), 457.42 unionselect '2002', convert(datetime,'9/10/2008',103), 495.79 unionselect '2002', convert(datetime,'10/10/2008',103), 65.95 unionselect '2002', convert(datetime,'11/10/2008',103), 313.36 )tleft join (select '1001' a,convert(datetime,'1/10/2008',103) b, 18.8 c unionselect '1001', convert(datetime,'2/10/2008',103) , 227.07 unionselect '1001', convert(datetime,'3/10/2008',103), 202.9 unionselect '1001', convert(datetime,'4/10/2008',103), 218.94 unionselect '1001', convert(datetime,'5/10/2008',103), 92.8 unionselect '1001', convert(datetime,'6/10/2008',103), 378.33 unionselect '1001', convert(datetime,'7/10/2008',103), 351.87 unionselect '1001', convert(datetime,'8/10/2008',103), 437.01 unionselect '1001', convert(datetime,'9/10/2008',103), 169.64 unionselect '1001', convert(datetime,'10/10/2008',103), 411.2 unionselect '1001', convert(datetime,'11/10/2008',103), 423.61 unionselect '2002', convert(datetime,'1/10/2008',103), 410.17 unionselect '2002', convert(datetime,'2/10/2008',103), 37.8 unionselect '2002', convert(datetime,'3/10/2008',103), 244.86 unionselect '2002', convert(datetime,'4/10/2008',103), 244.1 unionselect '2002', convert(datetime,'5/10/2008',103), 227.01 unionselect '2002', convert(datetime,'6/10/2008',103), 169.95 unionselect '2002', convert(datetime,'7/10/2008',103), 432.32 unionselect '2002', convert(datetime,'8/10/2008',103), 457.42 unionselect '2002', convert(datetime,'9/10/2008',103), 495.79 unionselect '2002', convert(datetime,'10/10/2008',103), 65.95 unionselect '2002', convert(datetime,'11/10/2008',103), 313.36 )t1on (t1.b=t.b-1 or t1.b=t.b-2 or t1.b=t.b-3 or t1.b=t.b-4 or t1.b=t.b-5 )and t.a=t1.agroup by t.a,t.b--having count(t1.a)=5order by 1,2 |
|
|
|
|
|