SQL Server:如何在同一个表中为不同的客户获得3天的滚动总和

时间:2015-08-26 22:34:48

标签: sql-server

这是输入表:

Customer_ID  Date       Amount
1            4/11/2014  20
1            4/13/2014  10
1            4/14/2014  30
1            4/18/2014  25
2            5/15/2014  15
2            6/21/2014  25
2            6/22/2014  35
2            6/23/2014  10

有关于多个客户的信息,我想在每个客户的3天窗口中获得滚动金额。 解决方案应如下所示:

Customer_ID  Date       Amount  Rolling_3_Day_Sum
1            4/11/2014  20      20
1            4/13/2014  10      30
1            4/14/2014  30      40
1            4/18/2014  25      25
2            5/15/2014  15      15
2            6/21/2014  25      25
2            6/22/2014  35      60
2            6/23/2014  10      70

最大的问题是我没有每天的交易,因为按行号分区不起作用。

我在SO上找到的最接近的例子是: SQL Query for 7 Day Rolling Average in SQL Server 但即使在这种情况下,每天都会进行交易,以适应基于rownumber()的解决方案

rownumber查询如下:

select customer_id, Date, Amount, 
Rolling_3_day_sum = CASE WHEN ROW_NUMBER() OVER (partition by customer_id ORDER BY Date) > 2
    THEN SUM(Amount) OVER (partition by customer_id ORDER BY Date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
    END
from #tmp_taml9
order by customer_id

我想知道是否有办法用“BETWEEN [DATE - 2]和[DATE]”取代“BETWEEN 2 PRECEDING AND CURRENT ROW”

1 个答案:

答案 0 :(得分:1)

一种选择是使用日历表(或类似的东西)来获取完整的日期范围,然后将表格与其连接并使用基于row_number的解决方案。

可能工作的另一个选项(不确定性能)将使用apply这样的查询:

select customer_id, Date, Amount, coalesce(Rolling_3_day_sum, Amount) Rolling_3_day_sum
from #tmp_taml9 t1 
cross apply (
    select sum(amount) Rolling_3_day_sum 
    from #tmp_taml9 
    where Customer_ID = t1.Customer_ID 
      and datediff(day, date, t1.date) <= 3 
      and t1.Date >= date
) o
order by customer_id;

我怀疑表现可能并不好。