Advertisement
kirzecy670

Untitled

Dec 20th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH
  2.     exp_users AS (
  3.         SELECT
  4.             ft.uid AS uid,
  5.             ft.action,
  6.             argMin(visitParamExtractString(ft.addJson, 'option'), dt) AS exp_group,
  7.             min(dt) AS exp_dt
  8.         FROM stat.funnelTrack AS ft
  9.         WHERE ft.dt >= '2024-12-01'
  10.           AND ft.action in ('pteam_appstat_v31', 'pteam_route_redesign')
  11.         GROUP BY 1, 2
  12.         HAVING exp_group IN ('old', 'new')
  13.     ),
  14.     contracts AS (
  15.         SELECT
  16.             uid,
  17.             action,
  18.             ContractID AS contract_id,
  19.             argMin(Product, ContractCharge_TM) AS product,
  20.             min(ContractCharge_TM) AS ts_charge
  21.         FROM analytics.money JOIN exp_users using(uid)
  22.         WHERE toTimeZone(ContractCharge_TM, 'GMT') >= '2023-01-01 00:00:00'
  23.           AND toTimeZone(ActiveFrom, 'GMT') >= '2023-01-01 00:00:00'
  24.           AND Type = 'subscription'
  25.           AND RUBGross NOT IN (1, 11)
  26.           AND ChargeNumber = 1
  27.           AND USDGross >= 0.25
  28.           AND BillingTypeGroup != 'partner'
  29.           AND uid != ''
  30.           AND ContractCharge_TM < exp_users.exp_dt
  31.         GROUP BY uid, contract_id, action
  32.     ),
  33.     states AS (
  34.         SELECT
  35.             ContractID AS contract_id,
  36.             State AS state,
  37.             TM AS ts_state
  38.         FROM analytics.ContractStateHistoryProduct
  39.         WHERE toTimeZone(TM, 'GMT') >= '2023-01-01 00:00:00'
  40.     ),
  41.     user_states AS (
  42.         SELECT
  43.             c.uid,
  44.             s.state,
  45.             s.ts_state,
  46.             c.ts_charge,
  47.             c.action
  48.         FROM contracts c
  49.         INNER JOIN states s USING (contract_id)
  50.     ),
  51.     last_state_before AS (
  52.         SELECT
  53.             eu.uid as uid,
  54.             action,
  55.             argMaxIf(state, ts_state, ts_state < eu.exp_dt) AS last_state_before
  56.         FROM user_states us
  57.         INNER JOIN exp_users eu ON us.uid = eu.uid
  58.         GROUP BY eu.uid, action
  59.     ),
  60.     last_state_after AS (
  61.         SELECT
  62.             eu.uid as uid,
  63.             action,
  64.             argMaxIf(state, ts_state, 1) AS last_state_after
  65.         FROM user_states us
  66.         INNER JOIN exp_users eu ON us.uid = eu.uid
  67.         GROUP BY eu.uid, action
  68.     )
  69. SELECT
  70.     eu.action,
  71.     eu.exp_group,
  72.     countDistinct(eu.uid) AS total_users,
  73.     countDistinctIf(eu.uid, last_state_before IN ('ACTIVE', 'WAIT_CONFIRM', 'CONFIRMED')) AS active_before,
  74.     countDistinctIf(eu.uid, last_state_after IN ('ACTIVE', 'WAIT_CONFIRM', 'CONFIRMED')) AS active_after,
  75.     active_after / active_before AS alive_rate,
  76.     active_before / total_users AS active_before_rate,
  77.     active_after / total_users AS active_after_rate,
  78.     (active_after - active_before) AS active_change_absolute,
  79.     (active_after_rate - active_before_rate) AS active_change_rate
  80. FROM exp_users eu
  81. LEFT JOIN last_state_before lsb ON eu.uid = lsb.uid and eu.action = lsb.action
  82. LEFT JOIN last_state_after lsa ON eu.uid = lsa.uid and eu.action = lsa.action
  83. GROUP BY
  84.     1, eu.exp_group
  85. ORDER BY
  86.     1, eu.exp_group;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement