简体   繁体   中英

Calculate Rolling retention with SQL (BigQuery)

I have a table of logins with such columns:

id - unique id of user
day - days passed since registration (0-30)

Each record in this table is a record of a user logged in, so there might be same rows (because user can log in multiple times a day). So I have to calculate how much users have logged in on some day of their life (30 days) or any other day later (rolling retention). Output table should contain columns with days (1-30) and amount of users. If user logged in on 30th day, we count him as retained on every day before 30. Any ideas? :)

Try this one:

select single_day, count(distinct id)
from mytable, unnest(generate_array(1, day)) as single_day
group by single_day

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.

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