Advertisement
AliaksandrLet

Реализация инкрементальной загрузки в коде Задание 1

May 21st, 2023 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- загрузка данных в таблицу
  2. INSERT INTO clients_cluster_metrics_m (
  3.     month,
  4.     client_id,
  5.     utm_campaign,
  6.     reg_code,
  7.     total_events,
  8.     visit_events,
  9.     registration_events,
  10.     login_events,
  11.     visit_to_login_events,
  12.     total_pay_events,
  13.     accepted_method_actions,
  14.     avg_payment,
  15.     made_payments,
  16.     sum_payments,
  17.     rejects_share
  18. )
  19. WITH activity_stats AS (
  20.     SELECT
  21.         date_trunc('month', hitdatetime)::date                                          AS "month",
  22.         client_id                                                                       AS client_id,
  23.         COUNT(1)                                                                        AS total_events,
  24.         SUM(CASE WHEN "action" = 'visit' THEN 1 ELSE 0 END)                             AS visit_events,
  25.         SUM(CASE WHEN "action" = 'registration' THEN 1 ELSE 0 END)                      AS registration_events,
  26.         SUM(CASE WHEN "action" = 'login' THEN 1 ELSE 0 END)                             AS login_events,
  27.         SUM(CASE WHEN "action" = 'login' AND prev_action = 'visit' THEN 1 ELSE 0 END)   AS visit_to_login_events
  28.     FROM (
  29.         SELECT
  30.             *,
  31.             LAG("action") OVER (PARTITION BY client_id ORDER BY hitdatetime ASC) AS prev_action
  32.         FROM
  33.             user_activity_log
  34.         WHERE
  35.             extract(year FROM hitdatetime) IN (2019, 2020)
  36.             AND "action" != 'N/A'
  37.         ) AS t
  38.     GROUP BY
  39.         1,
  40.         2
  41. ),
  42. payment_stats AS (
  43.     SELECT
  44.         date_trunc('month',hitdatetime)::date AS "month",
  45.         client_id AS client_id,
  46.         count(1) AS total_pay_events,
  47.         count(CASE WHEN "action" = 'accept-method' THEN 1 END) AS accepted_method_actions,
  48.         count(CASE WHEN "action" = 'make-payment' THEN 1 END) AS made_payments,
  49.         avg(CASE WHEN "action" = 'make-payment' THEN coalesce(payment_amount,0) ELSE 0 END) AS avg_payment,
  50.         sum(CASE WHEN "action" = 'make-payment' THEN coalesce(payment_amount,0) ELSE 0 END) AS sum_payments,
  51.         sum(CASE WHEN "action" = 'reject-payment' THEN coalesce(payment_amount, 0) ELSE 0 END)
  52.             / nullif(sum(CASE WHEN "action" = 'make-payment' THEN coalesce(payment_amount,0) ELSE 0 END), 0)    AS rejects_share
  53.     FROM
  54.         user_payment_log
  55.     WHERE
  56.         extract(year FROM hitdatetime) IN (2019, 2020)
  57.     GROUP BY
  58.         1,
  59.         2
  60. ),
  61. user_contacts_latest AS (
  62.     SELECT DISTINCT ON (client_id)
  63.         client_id,
  64.         SUBSTR(REGEXP_REPLACE(phone,'[^0123456789]','','g'),2,3) AS reg_code
  65.     FROM
  66.         user_contacts
  67.     ORDER BY
  68.         client_id ASC,
  69.         created_at DESC
  70. )
  71. SELECT
  72.     coalesce(a."month", p."month")          AS "month",
  73.     ua.client_id                            AS client_id,
  74.     ua.utm_campaign                         AS utm_campaign,
  75.     contacts.reg_code                       AS reg_code,
  76.     coalesce(a.total_events,0)              AS total_events,
  77.     coalesce(a.visit_events,0)              AS visit_events,
  78.     coalesce(a.registration_events, 0)      AS registration_events,
  79.     coalesce(a.login_events, 0)             AS login_events,
  80.     coalesce(a.visit_to_login_events, 0)    AS visit_to_login_events,
  81.     coalesce(p.total_pay_events, 0)         AS total_pay_events,
  82.     coalesce(p.accepted_method_actions, 0)  AS accepted_method_actions,
  83.     coalesce(p.avg_payment, 0)              AS avg_payment,
  84.     coalesce(p.made_payments, 0)            AS made_payments,
  85.     coalesce(p.sum_payments, 0)             AS sum_payments,
  86.     p.rejects_share                         AS rejects_share
  87. FROM
  88.     activity_stats AS a
  89.     FULL JOIN payment_stats AS p
  90.         ON p."month" = a."month" AND p.client_id = a.client_id
  91.     RIGHT JOIN user_attributes AS ua
  92.         ON ua.client_id = coalesce(a.client_id, p.client_id)
  93.     LEFT JOIN user_contacts_latest AS contacts
  94.         ON contacts.client_id = ua.client_id
  95. ORDER BY
  96.     1,
  97.     2;
  98.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement