Advertisement
kirzecy670

Untitled

Mar 19th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. WITH
  2. reinstall_users AS (
  3. SELECT DISTINCT uid
  4. FROM stat.funnelTrack
  5. WHERE dt BETWEEN '2024-01-01' AND '2026-01-01'
  6. AND (
  7. (
  8. action IN ('bind_user', 'rebind_user')
  9. AND visitParamExtractString(addJson, 'reason')
  10. IN ('email','loginByEmailAndCode','restoreByAdId','adid','moveGlobalToRu','migration')
  11. )
  12. OR action = 'reinstall_detected'
  13. )
  14. ),
  15. recent_users AS (
  16. SELECT
  17. uid,
  18. id,
  19. last_activity_date,
  20. __ts_install
  21. FROM
  22. (
  23. SELECT
  24. uid,
  25. MAX(dt) AS last_activity_date
  26. FROM
  27. analytics.parent_actions pa
  28. WHERE
  29. dt >= today() - INTERVAL '30 day'
  30. AND dt < today()
  31. GROUP BY uid
  32. ) AS p_actions
  33. INNER JOIN
  34. (
  35. SELECT
  36. uid,
  37. id,
  38. toTimeZone(min(install_datetime), 'GMT') AS __ts_install
  39. FROM analytics.parent_properties
  40. WHERE is_full_register
  41. AND full_reg_datetime >= today() - INTERVAL '30' DAY
  42. AND full_reg_datetime < today()
  43. [[AND {{region}}]]
  44. [[AND {{platform}}]]
  45. [[AND {{country}}]]
  46. AND region IN ('ru', 'global')
  47. AND platform IN ('iOS', 'Android')
  48. GROUP BY uid, id
  49. ) AS p_props
  50. USING(uid)
  51. ),
  52. second_parents AS (
  53. SELECT
  54. usedBy AS sp_id,
  55. usedAt AS ts_connect
  56. FROM wimc.mp_secondParentConnection
  57. WHERE sp_id != 0
  58. ),
  59. child_activity AS (
  60. SELECT
  61. id AS child,
  62. max(__dt_last_active) AS dt_last_active
  63. FROM analytics.uid_to_id AS utoi
  64. INNER JOIN (
  65. SELECT
  66. uid,
  67. max(dt) AS __dt_last_active
  68. FROM analytics.child_actions
  69. WHERE geo_session
  70. GROUP BY uid
  71. ) AS geo
  72. USING (uid)
  73. GROUP BY child
  74. ),
  75. parent_child_links AS (
  76. WITH
  77. arraySort(
  78. x -> x.1,
  79. groupArray(
  80. tuple(
  81. toTimeZone(ts_updated, 'GMT'),
  82. if(
  83. update_reason IN (
  84. 'user.deleteChildByRelationId',
  85. 'user.deleteChild',
  86. 'user._deletePersonalData',
  87. 'user._mergeChilds force child remove after cross-auth'
  88. ),
  89. 'delete',
  90. 'add'
  91. )
  92. )
  93. )
  94. ) AS updates,
  95. updates.2 AS updates_filtered
  96. SELECT
  97. parent,
  98. child,
  99. min(ts_updated) AS ts_link,
  100. countIf(updates_filtered[-1] != 'delete') OVER (PARTITION BY child ORDER BY ts_link) AS __n_parents,
  101. countIf(updates_filtered[-1] != 'delete') OVER (PARTITION BY child) AS __total_parents
  102. FROM logs.parent_child_history
  103. WHERE parent != 0
  104. AND child IS NOT NULL
  105. AND update_reason IN (
  106. 'user.addParentByCode',
  107. 'child merging in user._mergeChilds',
  108. 'watch.connectWatch',
  109. 'watch.connect',
  110. 'user._copyChilds',
  111. 'user.registerNewParentChild',
  112. 'user.getAccountByChildInvite',
  113. /* deletions */
  114. 'user.deleteChildByRelationId',
  115. 'user.deleteChild',
  116. 'user._deletePersonalData',
  117. 'user._mergeChilds force child remove after cross-auth'
  118. )
  119. GROUP BY parent, child
  120. ),
  121. parents_categorized AS (
  122. SELECT
  123. ru.id AS parent,
  124. any(__ts_install) AS ts_install,
  125. argMin(__total_parents, l.ts_link) AS total_parents,
  126. argMin(__n_parents, l.ts_link) AS n_parents
  127. FROM child_activity AS a
  128. INNER JOIN parent_child_links AS l ON toUInt64(l.child) = toUInt64(a.child)
  129. INNER JOIN recent_users AS ru ON toUInt64(ru.id) = toUInt64(l.parent)
  130. WHERE age('day', ts_link::Date, dt_last_active) >= 1 - {{ geo_lookback_window }}
  131. AND ts_link >= __ts_install
  132. GROUP BY parent
  133. ),
  134. user_metrics AS (
  135. SELECT
  136. pp.region,
  137. pp.platform,
  138. pp.full_reg_datetime,
  139. pp.uid as uid,
  140. itt.tier, itt.country, ri.uid = '' as flg_reinstall, pp.full_reg_datetime >= today() - interval 30 day as flg_new_user,
  141.  
  142. multiIf(
  143. pc.n_parents > 1, 'second+ parent',
  144. pc.n_parents = 1 AND pc.total_parents = 1, 'single parent',
  145. 'first parent'
  146. ) AS parent_category
  147. FROM
  148. analytics.parent_properties pp
  149. JOIN
  150. recent_users ru ON toUInt64(pp.id) = toUInt64(ru.id)
  151. INNER JOIN
  152. parents_categorized pc ON toUInt64(pp.id) = toUInt64(pc.parent)
  153. LEFT JOIN analytics.ipc_to_tier AS itt
  154. ON lower(itt.ipc) = pp.ip_country
  155. LEFT JOIN reinstall_users ri ON pp.uid = ri.uid
  156. WHERE
  157. pp.is_full_register
  158. AND pp.full_reg_datetime != '1970-01-01 00:00:00'
  159. AND pp.full_reg_datetime < today()
  160. [[AND {{region}}]]
  161. [[AND {{platform}}]]
  162. [[AND {{country}}]]
  163. AND pp.region IN ('ru', 'global')
  164. AND pp.platform IN ('iOS', 'Android')
  165. )
  166.  
  167. SELECT
  168. platform,
  169. parent_category,
  170. flg_reinstall,
  171. flg_new_user,
  172. tier,
  173. country,
  174. uniqCombined(uid) as Users,
  175. -- uniqCombined(uid) FILTER (WHERE parent_category = 'second+ parent') as Second_Parents,
  176. SUM(uniqCombined(uid)) OVER (PARTITION BY country) as Cohort,
  177. Users / Cohort AS Category_Share_of_Cohort
  178. FROM
  179. user_metrics
  180. GROUP BY
  181. platform,
  182. parent_category,
  183. flg_reinstall,
  184. flg_new_user,
  185. tier,
  186. country
  187. ORDER BY Category_Share_of_Cohort DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement