Skip to content
Advertisement

how to find consecutive user login across week

I’m fairly new to SQL & maybe the complexity level for this report is above my pay grade

I need help to figure out the list of users who are logging to the app consecutively every week in the time period chosen(this logic eventually needs to be extended to a month, quarter & year ultimately but a week is good for now)

Table structure for ref

events: User_id int, login_date timestamp

The table events can have 1 or more entries for a user. This inherently means that the user can login multiple times to the app. To shed some light, if we focus on Jan 2020- Mar2020 then I need the following in the output

  1. user_id who logged into the app every week from 2020wk1 to 2020Wk14 at least once
  2. the week they logged in
  3. number of times they logged in that week

I’m also okay if the output of the query is just the user_id. The thing is I’m unable to make sense out of the output that I’m seeing on my end after trying the following SQL code, perhaps working on this problem for so long might be the reason for that!

SQL code tried so far:

Advertisement

Answer

You can get the summary you want as:

But the filtering is tricker. It is better to go with dates rather than week numbers:

Then you can use a subquery:

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