Advertisement
kirzecy670

Untitled

Nov 26th, 2024 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH approved_parents AS (
  2.     SELECT
  3.         pp.uid AS parent_uid,
  4.         pp.platform as parent_platform,
  5.         pp.full_reg_datetime as parent_full_reg_datetime,
  6.         children.2 AS child_id,
  7.         children.3 AS child_status
  8.     FROM
  9.         analytics.parent_properties pp ARRAY JOIN children
  10.     WHERE
  11.         pp.is_full_register
  12.         AND pp.full_reg_datetime < today() - INTERVAL 7 DAY
  13.         AND pp.full_reg_datetime >= today() - INTERVAL 57 DAY
  14.         AND pp.ip_country = 'us'
  15. --        AND children.3 = 'approved'
  16. ),
  17. android_children AS (
  18.     SELECT
  19.         child_props.uid AS parent_uid,
  20.         child_props.id AS child_id
  21.     FROM
  22.         analytics.child_properties child_props
  23.     JOIN
  24.         approved_parents ap ON ap.child_id = child_props.id
  25.     WHERE
  26.         child_props.platform = 'Android'
  27. ),
  28. aggregated_app_usage AS (
  29.     SELECT
  30.         usage_data.dt AS usage_date,
  31.         usage_data.child AS child_id,
  32.         usage_data.appId AS app_id,
  33.         argMax(usageData, ts) AS max_usage_data
  34.     FROM
  35.         stat.appUsageWithAggregated usage_data
  36.     JOIN
  37.         android_children ac ON usage_data.child = ac.child_id
  38.     WHERE
  39.         usage_data.dt >= today() - INTERVAL 7 DAY
  40.         AND usage_data.dt < today()
  41.     GROUP BY
  42.         usage_date, child_id, app_id
  43. ),
  44. daily_usage_metrics AS (
  45.     SELECT
  46.         child_id,
  47.         SUM(visitParamExtractInt(max_usage_data, 'secondUsageDay')) / COUNT(DISTINCT usage_date) AS daily_usage_seconds
  48.     FROM
  49.         aggregated_app_usage
  50.     GROUP BY
  51.         child_id
  52.     HAVING
  53.         daily_usage_seconds BETWEEN 15000 and 21000
  54. ),
  55. child_routes_data AS (
  56.          SELECT user_id,
  57.                 SUM(route_distance)/uniqExact(start_dt) AS avg_daily_distance
  58.     FROM
  59.         analytics.child_routes_new crn
  60.         JOIN android_children ac on crn.user_id = ac.child_id
  61.     WHERE
  62.         start_dt >= today() - INTERVAL 14 DAY
  63.         AND start_dt < today()
  64.     GROUP BY 1
  65. --    HAVING avg_daily_distance BETWEEN 20000 and 40000/
  66. )
  67. SELECT
  68.     ap.parent_uid as parent_uid,
  69.     parent_platform,
  70.     parent_full_reg_datetime::date as parent_full_reg_date,
  71.     ap.child_id as child_id,
  72.     round(child_routes_data.avg_daily_distance) as avg_daily_distance_meters,
  73.     round(dum.daily_usage_seconds/3600, 1) as avg_daily_usage_hours
  74. FROM
  75.     approved_parents ap
  76.     JOIN daily_usage_metrics dum on ap.child_id = dum.child_id
  77.     LEFT JOIN child_routes_data on child_routes_data.user_id = ap.child_id
  78. ORDER BY parent_uid, child_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement