I have a table with user data and want to create a cumulative count distinct but this type of window function does not exist. This is my table
date | user-id | purchase-id
2020-01-01 | 1 | 244
2020-01-03 | 1 | 244
2020-02-01 | 1 | 524
2020-03-01 | 2 | 443
Now, I want a cum count distinct for purchase id like this:
date | user-id | purchase-id | cum_purchase
2020-01-01 | 1 | 244 | 1
2020-01-03 | 1 | 244 | 1
2020-02-01 | 1 | 524 | 2
2020-03-01 | 2 | 443 | 1
I tried
Select
dt,
user_id,
count(distinct purchase_id) over (partition by user_id ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as cum_ct
from table
I get an error that I cannot use count distinct with an order by statement. What to do?
Something like this
Select
dt as [date],
user_id,
purchase_id
SUM(CASE WHEN rn = 1 THEN 1 ELSE 0 END) over (partition by user_id ORDER BY dt ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as cum_ct
from (
SELECT
dt,
user_id,
purchase_id,
ROW_NUMBER() OVER (PARTITION BY user_id, purchase_id ORDER BY dt) as RN
FROM sometable
) sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.