H
H
HelloDarknessMyOldFried2021-04-07 18:58:30
PostgreSQL
HelloDarknessMyOldFried, 2021-04-07 18:58:30

How to calculate the cumulative sum of unique users?

Let's say I have the following frame:
day_no user_id 0
a
0 b
0 c
0 d
0 e
0 f
1 a
2 c
2 e
3 f
3 a
4 a

it turned out like this:
day_no sum_unique_users
4 1
3 2
2 4
1 4
0 6

How can this be calculated in postgresql?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-04-07
@galaxy

Well, something like this (if the rather redundant self-join does not bother you):

select t.day_no, count(distinct t2.user_id)
  from t
  join t as t2 on (t2.day_no >= t.day_no)
 group by 1
 order by 1 desc

sql fiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question