Moving sum in SQL server 2008 R2 -
edit: server i'm working on 2008 r2 client 2012...
i'm trying create conditionnal sum based on month i'm in backing 1 year ago. here data have:
month location quantity1 quantity2 200501 2072 46 42 200502 2072 53 51 200503 2072 141 132 200504 2072 134 129 200505 2072 75 72 200506 2072 75 74 200507 2072 77 72 200508 2072 56 52 200509 2072 58 54 200510 2072 59 52 200511 2072 71 69 200512 2072 48 46 200601 2072 62 55 200602 2072 209 197 200603 2072 143 138 200604 2072 78 67 200605 2072 95 86 200606 2072 78 73 200607 2072 80 73 200608 2072 68 62 200609 2072 54 46 200610 2072 52 49 200611 2072 50 50 200612 2072 28 27
so, output this: first month(200501) can in total there no records prior it, 200512 between starting month (200501) , month in question. once goes 200601, has sum between 200502 , 200601.
i did mannual calculations validation process, here are:
month location sumqty1 sumqty2 200501 2072 46 42 200502 2072 99 93 200503 2072 240 225 200504 2072 374 354 200505 2072 449 426 200506 2072 524 500 200507 2072 601 572 200508 2072 657 624 200509 2072 715 678 200510 2072 774 730 200511 2072 845 799 200512 2072 893 845 200601 2072 909 858 200602 2072 1065 1004 200603 2072 1067 1010 200604 2072 1011 948 200605 2072 1031 962 200606 2072 1034 961 200607 2072 1037 962 200608 2072 1049 972 200609 2072 1045 964 200610 2072 1038 961 200611 2072 1017 942 200612 2072 997 923
thank time!
you can join table dates need added:
select t1.month , t1.location , sum(t2.quantity1) sumqty1 , sum(t2.quantity2) sumqty2 dbname.schemaname.tablename t1 inner join dbname.schemaname.tablename t2 on t2.month >= t1.month - 99 , t2.month <= t1.month group t1.month , t1.location
Comments
Post a Comment