SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with t1 as (
  2.  
  3. SELECT  *,
  4. lag(timestamp) over(partition by user_id order by timestamp) as timestamp_lag,
  5. if(TIMESTAMP_DIFF(timestamp, lag(timestamp) over(partition by user_id order by timestamp),minute)<30,0,1) as session_start
  6. FROM `table_name`
  7.  
  8. ), t2 as (
  9.  
  10. select *,
  11. sum(session_start) over(partition by user_id order by timestamp) as session_number
  12. from t1
  13.  
  14. ), t3 as (
  15.  
  16. select *,
  17. concat(user_id,'_',cast(session_number as string)) as session_id,
  18. min(timestamp) over(partition by user_id, session_number) as start_session_time,
  19. max(timestamp) over(partition by user_id, session_number) as end_session_time
  20. from t2
  21.  
  22. )
  23.  
  24. select user_id, session_id, start_session_time, end_session_time
  25. from t3
  26. group by 1,2,3,4
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top