Guest User

Untitled

a guest
Nov 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. CREATE MATERIALIZED VIEW stats.daily_dashboard_user_report AS(
  2. WITH new_users_day_before_today_cte AS(
  3. SELECT
  4. total AS new_users_day_before_today
  5. FROM
  6. newUsersDaily
  7. ORDER BY
  8. date_time DESC
  9. LIMIT
  10. 1
  11. OFFSET
  12. 1
  13. ), new_users_today_cte AS(
  14. SELECT
  15. total AS new_users_today
  16. FROM
  17. newUsersDaily
  18. ORDER BY
  19. date_time DESC
  20. LIMIT
  21. 1
  22. ), new_accounts_today_cte AS(
  23. SELECT
  24. count(*) AS new_accounts_today
  25. FROM
  26. usersAndTokens
  27. WHERE
  28. created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour') - '8h'::interval
  29. LIMIT
  30. 1
  31. ), new_accounts_before_today_cte AS(
  32. SELECT
  33. count(*) AS new_accounts_before_today
  34. FROM
  35. usersAndTokens
  36. WHERE
  37. created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-2 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval
  38. LIMIT
  39. 1
  40. ), new_avg_accounts_before_today_cte AS(
  41. SELECT
  42. (count(*) / 7)::int as new_avg_accounts_before_today
  43. FROM
  44. usersAndTokens
  45. WHERE
  46. created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-8 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval
  47. LIMIT
  48. 1
  49. ), new_accounts_weekday_before_cte AS(
  50. SELECT
  51. count(*) AS new_accounts_weekday_before
  52. FROM
  53. usersAndTokens
  54. WHERE
  55. created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-8 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-7 day') - '1s 8h'::interval
  56. LIMIT
  57. 1
  58. ),new_matches_before_today_cte AS(
  59. SELECT
  60. total AS new_matches_before_today
  61. FROM
  62. newMatchesDaily
  63. ORDER BY
  64. date_time DESC
  65. LIMIT
  66. 1
  67. OFFSET
  68. 1
  69. ), new_matches_today_cte AS(
  70. SELECT
  71. total AS new_matches_today
  72. FROM
  73. newMatchesDaily
  74. ORDER BY
  75. date_time DESC
  76. LIMIT
  77. 1
  78. ), total_users_today_cte AS(
  79. SELECT
  80. totalRegistredUsers.total - COALESCE( totalRegistredHiddenUsers.total, 0) AS non_hidden_users_total
  81. FROM
  82. totalRegistredUsers
  83. LEFT JOIN
  84. totalRegistredHiddenUsers USING(date_time)
  85. ORDER BY
  86. date_time DESC
  87. LIMIT
  88. 1
  89. ), total_accounts_today_cte AS(
  90. SELECT
  91. total AS total_accounts_today
  92. FROM
  93. totalRegistredUsers
  94. ORDER BY
  95. date_time DESC
  96. LIMIT
  97. 1
  98. ), daily_active_users_today_cte AS(
  99. SELECT
  100. total AS daily_active_users_today
  101. FROM
  102. dailyActiveUsers
  103. ORDER BY
  104. date_time DESC
  105. LIMIT
  106. 1
  107. ), daily_active_users_before_today_cte AS(
  108. SELECT
  109. total AS daily_active_users_before_today
  110. FROM
  111. dailyActiveUsers
  112. ORDER BY
  113. date_time DESC
  114. LIMIT
  115. 1
  116. OFFSET
  117. 1
  118. ), weekly_active_users_today_cte AS(
  119. SELECT
  120. total AS weekly_active_users_today
  121. FROM
  122. weeklyActiveUsers
  123. ORDER BY
  124. date_time DESC
  125. LIMIT
  126. 1
  127. ), weekly_active_users_before_today_cte AS(
  128. SELECT
  129. total AS weekly_active_users_before_today
  130. FROM
  131. weeklyActiveUsers
  132. ORDER BY
  133. date_time DESC
  134. LIMIT
  135. 1
  136. OFFSET
  137. 1
  138. ), monthly_active_users_today_cte AS(
  139. SELECT
  140. total AS monthly_active_users_today
  141. FROM
  142. monthlyActiveUsers
  143. ORDER BY
  144. date_time DESC
  145. LIMIT
  146. 1
  147. ), monthly_active_users_before_today_cte AS(
  148. SELECT
  149. total AS monthly_active_users_before_today
  150. FROM
  151. monthlyActiveUsers
  152. ORDER BY
  153. date_time DESC
  154. LIMIT
  155. 1
  156. OFFSET
  157. 1
  158. )
  159. SELECT
  160. new_accounts_today,
  161. new_users_today,
  162. new_matches_today,
  163. non_hidden_users_total,
  164. total_accounts_today,
  165. daily_active_users_today,
  166. weekly_active_users_today,
  167. monthly_active_users_today,
  168. 100*new_accounts_today::numeric/NULLIF(new_accounts_before_today, 0) - 100 AS new_acc_percent,
  169. 100*new_accounts_today::numeric/NULLIF(new_accounts_weekday_before, 0) - 100 AS new_acc_weekday_percent,
  170. 100*new_accounts_today::numeric/NULLIF(new_avg_accounts_before_today, 0) - 100 AS new_avg_acc_percent,
  171. 100*new_users_today::numeric/NULLIF(new_users_day_before_today, 0) - 100 AS tru_percent,
  172. 100*new_matches_today::numeric/NULLIF(new_matches_before_today, 0) - 100 AS matches_percent,
  173. 100*daily_active_users_today::numeric/NULLIF(daily_active_users_before_today, 0) - 100 AS dau_percent,
  174. 100*weekly_active_users_today::numeric/NULLIF(weekly_active_users_before_today, 0) - 100 AS wau_percent,
  175. 100*monthly_active_users_today::numeric/NULLIF(monthly_active_users_before_today, 0) - 100 AS mau_percent
  176. FROM
  177. new_users_day_before_today_cte,
  178. new_users_today_cte,
  179. new_accounts_today_cte,
  180. new_accounts_before_today_cte,
  181. new_avg_accounts_before_today_cte,
  182. new_accounts_weekday_before_cte,
  183. new_matches_before_today_cte,
  184. new_matches_today_cte,
  185. total_users_today_cte,
  186. total_accounts_today_cte,
  187. daily_active_users_today_cte,
  188. daily_active_users_before_today_cte,
  189. weekly_active_users_today_cte,
  190. weekly_active_users_before_today_cte,
  191. monthly_active_users_today_cte,
  192. monthly_active_users_before_today_cte
  193. )
Add Comment
Please, Sign In to add comment