达到阈值后将滚动总和重置为0

时间:2019-05-23 09:29:14

标签: sql hive hiveql

我正在尝试计算运行总计,并根据2种情况或达到限制将其重置为0。

这里是一个例子。

Table

如上图所示,我需要在满足以下条件的情况下获得运行总计:

  1. 每月折扣= 0,月票== 1

    如果Discount = 1和Ticket = 0之一,则下一个运行总计的值必须为0。

  2. running_total <50

    如果运行总计> = 50,则运行总计的值必须从同一行的值开始。

这就是我现在想要做的:

在HIVE中是否有可能这样做?非常感谢!!!

TextInput

2 个答案:

答案 0 :(得分:0)

您的报告中根本没有考虑到40、60和20的事项吗?就像您希望对它们进行计数,然后添加新行(总计0来重新启动)一样?

答案 1 :(得分:0)

这是我设法做到的方式:

SELECT *,
       SUM(case when month_disc=1 OR month_ticket=0 then 0 else value end) OVER (PARTITION BY account, flg_sum, band_sum ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_sum
 FROM (
    SELECT *,
           FLOOR(SUM(case when month_disc=1 OR month_ticket=0 then 0 else value end) OVER (PARTITION BY account, flg_sum ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)/50.000001) as band_sum ---- create bands for running total
 FROM (
    SELECT *,
          SUM(tag_flg) OVER (PARTITION BY account ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS flg_sum
 FROM (
    SELECT *,
          CASE WHEN  (month_disc=1 OR month_ticket=0) THEN 1 ELSE 0 END AS tag_flg ---- flag to count when the value is reset due to one of the conditions
    FROM source_table) x ) y) z