Data Science

How do I use SQL Window Functions to calculate a 7-day rolling average for user retention?

AM Asked by Amanda Stevens · 12-03-2025
0 upvotes 14,277 views 0 comments
The question

I’m working with a massive dataset of daily user logins. I need to calculate a 7-day rolling average for retention to spot trends, but my current self-join is incredibly slow. Is there a more efficient way to use window functions like AVG() OVER() to handle this without crashing my BigQuery instance?

3 answers

0
JE
Answered on 15-03-2025

Using AVG() OVER() with a ROWS BETWEEN clause is the industry standard for this. For a 7-day average, use: AVG(login_count) OVER(ORDER BY login_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW). This avoids the Cartesian product of a self-join and is significantly lighter on memory. I implemented this for a fintech client last year and reduced their processing time from 40 minutes to under 3 minutes. Make sure your date column is indexed, or even better, partitioned by month if you are using a cloud-native warehouse like BigQuery or Snowflake for better cost efficiency.

0
DA
Answered on 18-03-2025

Are you accounting for gaps in dates where no users logged in, or does your dataset have a continuous row for every single calendar day?

CH 20-03-2025

That’s a great point, David. If there are missing days, the ROWS BETWEEN logic will actually average the last 7 records, not the last 7 days. To fix this, I usually use a recursive CTE to generate a date backbone first and then left join the login data. Does that sound like too much overhead for a dataset with 100 million rows, or is there a more native SQL way to handle date gaps?

0
MI
Answered on 22-03-2025

Window functions are definitely the way to go. Just ensure you are using ORDER BY within the over clause, or the result will be non-deterministic and useless.

AM 24-03-2025

I totally agree with Michael. Without the proper ordering, the window frame doesn't know the temporal sequence, which is the most common mistake I see.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session