Cumulative distinct count

前端 未结 4 1619
北海茫月
北海茫月 2021-02-06 14:37

I am working on query to get cumulative distinct count of uids on daily basis.

Example : Say there are 2 uids (100,200) appeared on date 2016-11-01 and they also appea

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 15:24

    You can use exists to check if an id was present on any of the previous dates. Then get the running sum and find the max value for each group which would get you the daily distinct cumulative count.

    select dt, max(col) as daily_cumulative_count
    from (select t1.*, 
          sum(case when not exists (select 1 from t where t1.dt > dt and id = t1.uid) then 1 else 0 end) over(order by dt) col
          from t t1) x 
    group by dt
    

提交回复
热议问题