我在下面有一个表格
Date | Customer | Count | Daily_Count | ITD_Count
d1 | A | 3 | 3 |
d2 | B | 4 | 4 |
d3 | A | 7 | 16 |
d3 | B | 9 | 16 |
d4 | A | 8 | 9 |
d4 | B | 1 | 9 |
字段的描述:
日期:日期
客户:客户名称
数:客户数量 daily_Count:每日客户数量计算为
SUM(count) OVER (partition BY date )as Daily_Count
问题:
如何计算ITD_Count中的运行总计或滚动总计?
输出应该看起来像
Date | Customer | Count | Daily_Count | ITD_Count
d1 | A | 3 | 3 | 3
d2 | B | 4 | 4 | 7
d3 | A | 7 | 16 | 23
d3 | B | 9 | 16 | 23
d4 | A | 8 | 9 | 31
d4 | B | 1 | 9 | 31
我尝试过使用Window功能的几种变体。但是在我所有的尝试中都遇到了障碍。
尝试1;
SUM(daily_COunt) OVER (partition BY date order by date rows between unbounded preceding and current row ) as ITD_account_linking
尝试2:
SUM(daily_COunt) OVER (partition BY date, daily_count order by date rows between unbounded preceding and current row ) as ITD_account_linking
以及此后的几次尝试。 :(
欢迎任何可能的建议,以指导我正确的方向。
如果您需要更多详细信息,请与我们联系。
答案 0 :(得分:0)
使用Hive窗口和分析功能。
SELECT Date, Customer, Count, Daily_Count,
SUM(Daily_Count) OVER (ORDER BY Date ROWS UNBOUNDED PRECEDING) AS ITD_Count
FROM table;