滚动12个月数据求和操作

时间:2019-07-17 20:17:08

标签: mysql sql sum rolling-computation

我已经看到平均数和计数都滚动了12,但不是总和。

我有一个像这样的桌子

ID | Profile_ID | EntryLocation | EntryMonth   |    Usage 
----------------------------------------------------------- 
1  |    1       |   11          |   2019-06-01 |    60.00  
2  |    1       |   12          |   2019-05-01 |    40.00  
3  |    1       |   11          |   2019-05-01 |    30.00  
4  |    2       |   11          |   2019-06-01 |   100.00 
5  |    1       |   12          |   2019-06-01 |    74.00  
6  |    2       |   12          |   2019-06-01 |   456.00 
7  |    2       |   11          |   2019-05-01 |    33.00 
8  |    1       |   11          |   2019-07-01 |   200.00 
9  |    1       |   11          |   2018-06-01 |   100.00 

我想做的是总结前几个月的使用情况,归类为Profile_IDEntryLocationEntryMonth(它们组成了集群主键(即这三者的组合是唯一的。

所以我要寻找的结果如下:

ID | Profile_ID | EntryLocation | EntryMonth   |    Rolling12Usage 
-----------------------------------------------------------
1  |    1       |   11          |   2018-06-01 |   100.00  
2  |    1       |   11          |   2019-05-01 |   130.00 --> (100 + 30) 
3  |    1       |   11          |   2019-06-01 |    90.00 -->(30 + 60 (note the 100 fell off))
4  |    1       |   11          |   2019-07-01 |   290.00 -->(90 + 200)
5  |    1       |   12          |   2019-05-01 |    40.00  
6  |    1       |   12          |   2019-06-01 |   114.00 -->(40 + 74)
7  |    2       |   11          |   2019-05-01 |    33.00  
8  |    2       |   11          |   2019-06-01 |   133.00 -->(33 + 100)
9  |    2       |   12          |   2019-06-01 |   456.00 

我已经尝试了分组

select x.Profile_ID, x.EntryLocation, x.EntryMonth, sum(Usage) as [SumUsage],
       (sum(sum(Usage)) over (order by x.EntryMonth rows between 11 preceding and current row) 
       ) as Sum_12month
from my_table_with_values x
group by x.EntryMonth, x.Profile_ID, x.EntryLocation
order by x.EntryMonth

但是无法正确订购。有任何想法/帮助吗?

如果您想添加解决方案,而无需在这些值上假设使用集群主键,则奖励积分(我敢打赌这种情况可能与我的情况更为普遍。

2 个答案:

答案 0 :(得分:1)

这是您要寻找的吗?

SELECT x.Profile_ID, 
       x.EntryLocation,
       x.Entry_Month,
       SUM(x2.Usage)
  FROM my_table_with_values x
  LEFT
  JOIN my_table_with_values x2
    ON x.Profile_id = x2.Profile_id
   AND x.Entry_Location x2.Entry_Location
   AND x2.Entry_Month BETWEEN DATEADD(month, -11, x.entrymonth)
        And x.entrymonth 
 GROUP
    BY x.Profile_ID, 
       x.EntryLocation,
       x.Entry_Month

答案 1 :(得分:0)

我认为您需要一个partition by

select x.Profile_ID, x.EntryLocation, x.EntryMonth, sum(Usage) as [SumUsage],
       sum(sum(Usage)) over (partition by Profile_ID, EntryLocation
                             order by x.EntryMonth
                             rows between 11 preceding and current row
                            ) as Sum_12month
from my_table_with_values x
group by x.EntryMonth, x.Profile_ID, x.EntryLocation
order by x.EntryMonth;

如果您没有全部月份,则可以使用range代替rows

select x.Profile_ID, x.EntryLocation, x.EntryMonth, sum(Usage) as [SumUsage],
       sum(sum(Usage)) over (partition by Profile_ID, EntryLocation
                             order by x.EntryMonth
                             range between interval 11 month preceding and current row
                            ) as Sum_12month
from my_table_with_values x
group by x.EntryMonth, x.Profile_ID, x.EntryLocation
order by x.EntryMonth;