如何计算运行总和如果标签< >0和重置为0如果标签=0在配置单元?

htzpubme  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(358)
customer    txn_date    tag running_sum
A           1-Jan-17    1   1
A           2-Jan-17    1   2
A           3-Jan-17    1   3
A           4-Jan-17    1   4
A           5-Jan-17    1   5
A           6-Jan-17    1   6
A           7-Jan-17    0   0
A           8-Jan-17    1   1
A           9-Jan-17    1   2
A           10-Jan-17   1   3
A           11-Jan-17   0   0
A           12-Jan-17   0   0
A           13-Jan-17   1   1
A           14-Jan-17   1   2
A           15-Jan-17   0   0

如果tag=0,如何获取运行总和并将运行总和重置为零?就像上面的例子一样。短暂性脑缺血发作

tpxzln5u

tpxzln5u1#

您需要做的是为1和0的每个部分创建“组”。您可以通过创建一个布尔标志,然后对该列进行累加求和来获得组。从那里你可以累积你的原始数据 tag 按在子查询中创建的每个组列出。
查询:

SELECT customer
  , txn_date
  , tag
  , SUM(tag) OVER (PARTITION BY customer, flg_sum ORDER BY txn_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_sum
FROM (
  SELECT *
    , SUM(tag_flg) OVER (PARTITION BY customer ORDER BY txn_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS flg_sum
  FROM (
    SELECT *
      , CASE WHEN  tag = 1 THEN 0 ELSE 1 END AS tag_flg
    FROM database.table ) x ) y

输出:

customer        txn_date        tag     running_sum
A               2017-01-01      1       1
A               2017-01-02      1       2
A               2017-01-03      1       3
A               2017-01-04      1       4
A               2017-01-05      1       5
A               2017-01-06      1       6
A               2017-01-07      0       0
A               2017-01-08      1       1
A               2017-01-09      1       2
A               2017-01-10      1       3
A               2017-01-11      0       0
A               2017-01-12      0       0
A               2017-01-13      1       1
A               2017-01-14      1       2
A               2017-01-15      0       0

相关问题