Skip to content
Advertisement

SQL 30 day active user query

I have a table of users and how many events they fired on a given date:

DATE USERID EVENTS
2021-08-27 1 5
2021-07-25 1 7
2021-07-23 2 3
2021-07-20 3 9
2021-06-22 1 9
2021-05-05 1 4
2021-05-05 2 2
2021-05-05 3 6
2021-05-05 4 8
2021-05-05 5 1

I want to create a table showing number of active users for each date with active user being defined as someone who has fired an event on the given date or in any of the preceding 30 days.

DATE ACTIVE_USERS
2021-08-27 1
2021-07-25 3
2021-07-23 2
2021-07-20 2
2021-06-22 1
2021-05-05 5

I tried the following query which returned only the users who were active on the specified date:

I also tried using a window function with rows between but seems to end up getting the same result:

I’m using SQL:ANSI on Snowflake. Any suggestions would be much appreciated.

Advertisement

Answer

This is tricky to do as window functions — because count(distinct) is not permitted. You can use a self-join:

However, that can be expensive. One solution is to “unpivot” the data. That is, do an incremental count per user of going “in” and “out” of active states and then do a cumulative sum:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement