繁体   English   中英

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM