Working in Standard SQL/BQ, but trying to figure out a way to get the average difference between two timestamps for each user for consecutive dates?
My data looks like:
UserId Login
0 2016-07-14 14:43:30 +0000
2 2018-07-09 17:19:55 +0000
2 2018-07-10 20:38:37 +0000
2 2014-07-12 17:37:20 +0000
2 2018-07-13 01:08:20 +0000
3 2015-12-04 17:17:41 +0000
3 2015-12-04 20:14:43 +0000
So at this point I have:
SELECT UserId
,CASE WHEN(
(Magic)
THEN AVG(TIMESTAMP_DIFF(Login2, Login, HOUR) ELSE NULL END))) AS hours
FROM TABLE
GROUP BY 1
The (Magic) portion is the stuck portion where I'd like to specify if the dates for each user are consecutive, then take the difference of the latest login date and subtract it by the current login date. Could I apply a lag and say if the difference between the next date and the first date is within a day then that's consecutive? Or is there a partition/window function approach to this?
Login2 would be the second login timestamp of the following consecutive date.
Results that I'd like:
UserId Average Hours between Consecutive Logins
0 NULL
2 17.5 hours
3 3 hours
- For UserId 0, it's NULL because there's no second login time to compare to.
- For UserId 2, it's getting the average between the difference in the two consecutive login dates (07/09 - 07/10 & 07/12 - 07/13) (27 hours + 8 hours / 2) = 5.5 hours
- For UserId 3, one data point with a same day login with 3 hours difference so it fits the consecutive condition, and got the average difference of the one data point