问题描述
我在Clickhouse中有以下数据:
每个用户在一天中的最终点是从开始到当天的总和(点)。 例如,用户1在2020-07-02中的点是800,而在2020-07-03中的点是200。
我需要这个结果:每个用户每天的积分:
解决方法
select uid,d,t from (
select uid,groupArray(date) dg,arrayCumSum(groupArray(spt)) gt from
(select uid,date,sum(pt) spt from
(select 1 tid,'2020-07-01' date,1 uid,500 pt
union all
select 1 tid,'2020-07-02' date,300 pt
union all
select 1 tid,'2020-07-03' date,-600 pt)
group by uid,date
order by uid,date)
group by uid) array join dg as d,gt as t
┌─uid─┬─d──────────┬───t─┐
│ 1 │ 2020-07-01 │ 500 │
│ 1 │ 2020-07-02 │ 800 │
│ 1 │ 2020-07-03 │ 200 │
└─────┴────────────┴─────┘