滚动总和以按产品计算每个月的 YTD 并使用 SQL 保存到单独的列

rolling sum to calculate YTD for each month group by product and save to separate columns using SQL

我有这样的数据:

Order_No  Product Month   Qty
3001       r33     1       8
3002       r34     1       11
3003       r33     1       17
3004       r33     2       3
3005       r34     2       11
3006       r34     3       1
3007       r33     3       -10
3008       r33     3       18

我想计算产品和每个月的年初至今总数量,并保存到单独的列中。下面是我想要的

Product  Qty_sum_jan    Qty_sum_feb    Qty_sum_mar
  r33         25              28             36
  r34         11              22             23

我知道如何使用 window 函数来计算滚动总和,但我不知道如何将它们分组到单独的列中。我目前使用这样的东西:

case when Month = 1 then sum(Qty) over(partition by Product order by Month) else 0 end as Qty_sum_jan,
case when Month <=2 then sum(Qty) over(partition by Product order by Month) else 0 end as Qty_sum_feb,
case when Month <=3 then sum(Qty) over(partition by Product order by Month) else 0 end as Qty_sum_mar,

这将使我按订单滚动总和,但如何达到我上面显示的产品级别?如果我使用 group by 那么它会抛出一个错误,因为 Month 不在 group by 子句中。我也不能只使用 max 来获取最后一个值,因为 qty 可以是负数,所以最后一个值可能不是最大值。我顺便用sparkSQL

据我了解,没有必要使用window函数。以下查询实现了您想要的输出:

select
    product,
    sum(case when month = 1 then qty else 0 end) as sum_qty_jan,
    sum(case when month <= 2 then qty else 0 end) as sum_qty_feb,
    sum(case when month <= 3 then qty else 0 end) as sum_qty_mar
from your_table
group by 1;

输出:

product sum_qty_jan sum_qty_feb sum_qty_mar
r33 25 28 36
r34 11 22 23