Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. with UserData as (
  2. select ud.*, date_trunc('day', c.timestamp) date
  3. from user_data ud
  4. join cookies c on ud.cookie_id = c.cookie_id
  5. where ud.email not in (
  6. 'matthieu@com.com', 'jared.j.burgess@gmail.com', 'nicolas.omeyer@gmail.com', 'matthieu.a.louis@gmail.com',
  7. 'omeyeralexandre@gmail.com', 'jared@stepsize.com', 'nick@stepsize.com', 'alex@stepsize.com', 'matt@stepsize.com'
  8. ) or ud.email is null
  9. )
  10.  
  11. , UsageIndicator as (
  12. select distinct cookie_id, date
  13. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  14. )
  15.  
  16. , FirstDay as (
  17. select cookie_id, min(date) date
  18. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  19. group by cookie_id
  20. )
  21.  
  22. , ShowCount as (
  23. select cookie_id, date, count(cookie_id) as show
  24. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  25. where activity_type_id = 2
  26. group by cookie_id, date
  27. )
  28.  
  29. , SearchCount as (
  30. select *, row_number() over (partition by cookie_id order by date) days_active
  31. from (
  32. select cookie_id, date, count(cookie_id) as search
  33. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  34. where activity_type_id = 3
  35. group by cookie_id, date
  36. ) bar
  37. )
  38.  
  39. , ExpandedCount as (
  40. select cookie_id, date, count(cookie_id) expanded
  41. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  42. where activity_type_id = 4
  43. group by cookie_id, date
  44. )
  45.  
  46. , CommitClickedCount as (
  47. select cookie_id, date, count(cookie_id) clicked
  48. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  49. where activity_type_id = 5
  50. group by cookie_id, date
  51. )
  52.  
  53. , TaskClickedCount as (
  54. select cookie_id, date, count(cookie_id) clicked
  55. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  56. where activity_type_id = 18
  57. group by cookie_id, date
  58. )
  59.  
  60. , FilterResultsCount as (
  61. select cookie_id, date, count(cookie_id) filter_results
  62. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  63. where activity_type_id = 9
  64. group by cookie_id, date
  65. )
  66.  
  67. , TimeoutCount as (
  68. select cookie_id, date, count(cookie_id) timeout
  69. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  70. where activity_type_id = 10
  71. group by cookie_id, date
  72. )
  73.  
  74. , SlackMessageCount as (
  75. select cookie_id, date, count(cookie_id) slack_message
  76. from (select *, date_trunc('day', utc_timestamp) date from desktop_activity) foo
  77. where activity_type_id = 14
  78. group by cookie_id, date
  79. )
  80.  
  81.  
  82. select
  83. case extract(isodow from ui.date)
  84. when 1 then 'Mon ' || cast(ui.date as date)
  85. when 2 then 'Tue ' || cast(ui.date as date)
  86. when 3 then 'Wed ' || cast(ui.date as date)
  87. when 4 then 'Thu ' || cast(ui.date as date)
  88. when 5 then 'Fri ' || cast(ui.date as date)
  89. when 6 then 'Sat ' || cast(ui.date as date)
  90. when 7 then 'Sun ' || cast(ui.date as date)
  91. end date,
  92. ud.id,
  93. case
  94. when fd.date = ui.date then true
  95. else false
  96. end first_day,
  97. case
  98. when sec.days_active is null then 'no search'
  99. else sec.days_active::text
  100. end days_active,
  101. ud.email,
  102. coalesce(shc.show, 0) as show,
  103. coalesce(sec.search, 0) as search,
  104. coalesce(ec.expanded, 0) expanded,
  105. coalesce(ccc.clicked, 0) commit_clicked,
  106. coalesce(tcc.clicked, 0) task_clicked,
  107. coalesce(frc.filter_results, 0) filter_results,
  108. coalesce(smc.slack_message, 0) slack_message,
  109. coalesce(tc.timeout, 0) timeout
  110.  
  111. from UserData ud
  112. left join UsageIndicator ui on ud.cookie_id = ui.cookie_id
  113. left join FirstDay fd on ui.cookie_id = fd.cookie_id
  114. left join ShowCount shc on ui.cookie_id = shc.cookie_id and ui.date = shc.date
  115. left join SearchCount sec on ui.cookie_id = sec.cookie_id and ui.date = sec.date
  116. left join ExpandedCount ec on ui.cookie_id = ec.cookie_id and ui.date = ec.date
  117. left join CommitClickedCount ccc on ui.cookie_id = ccc.cookie_id and ui.date = ccc.date
  118. left join TaskClickedCount tcc on ui.cookie_id = tcc.cookie_id and ui.date = tcc.date
  119. left join FilterResultsCount frc on ui.cookie_id = frc.cookie_id and ui.date = frc.date
  120. left join TimeoutCount tc on ui.cookie_id = tc.cookie_id and ui.date = tc.date
  121. left join SlackMessageCount smc on ui.cookie_id = smc.cookie_id and ui.date = smc.date
  122.  
  123. order by ui.date desc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement