简体   繁体   English

SQL/Presto:如何在 ds 上获取项目开始时间的累积总和

[英]SQL/Presto: how to get cumulative sum for a startime of items over ds

I have a data with the following format.我有以下格式的数据。 I want to count how many items have been created over time.我想计算随着时间的推移创建了多少项目。

-- 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

the desired result is:期望的结果是:

   cum_count  ds
    1          1/1/2021
    2          1/3/2021
    4          1/4/2021
    5          1/6/2021

I think i should get something with window functions but get confused in the partion by and order by part.我想我应该用 window 函数得到一些东西,但是在按部分排序和按部分排序时会感到困惑。

What I have for now is我现在拥有的是

select ds, count(start_time) over (partition by user_id order by ds) as cum_count 
from table1

but I do not need user level specific cum_count now.但我现在不需要特定于用户级别的 cum_count 。

I think you want aggregation as well as a cumulative sum:我认为您想要聚合以及累积总和:

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