SQL/Presto: 如何通过 ds 获得项目启动时间的累计总和
SQL/Presto: how to get cumulative sum for a startime of items over ds
我有一个格式如下的数据。我想统计随着时间的推移创建了多少项目。
-- table1
user_id item_id start_time ds
1 1 1/2/2021 1/1/2021
1 2 1/1/2021 1/3/2021
2 5 1/4/2021 1/4/2021
2 3 1/4/2021 1/4/2021
2 4 1/5/2021 1/6/2021
期望的结果是:
cum_count ds
1 1/1/2021
2 1/3/2021
4 1/4/2021
5 1/6/2021
我想我应该得到一些具有 window 功能的东西,但在按部分划分和按部分排序时感到困惑。
我现在有的是
select ds, count(start_time) over (partition by user_id order by ds) as cum_count
from table1
但我现在不需要用户级别特定 cum_count。
我想你想要聚合以及累加和:
select ds, count(*),
sum(count(*)) over (order by ds) as running_count
from table1
group by ds
order by ds;
我有一个格式如下的数据。我想统计随着时间的推移创建了多少项目。
-- table1
user_id item_id start_time ds
1 1 1/2/2021 1/1/2021
1 2 1/1/2021 1/3/2021
2 5 1/4/2021 1/4/2021
2 3 1/4/2021 1/4/2021
2 4 1/5/2021 1/6/2021
期望的结果是:
cum_count ds
1 1/1/2021
2 1/3/2021
4 1/4/2021
5 1/6/2021
我想我应该得到一些具有 window 功能的东西,但在按部分划分和按部分排序时感到困惑。
我现在有的是
select ds, count(start_time) over (partition by user_id order by ds) as cum_count
from table1
但我现在不需要用户级别特定 cum_count。
我想你想要聚合以及累加和:
select ds, count(*),
sum(count(*)) over (order by ds) as running_count
from table1
group by ds
order by ds;