Advertisement
GuestRT

khorvino_maxima

Jul 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.09 KB | None | 0 0
  1. CREATE TABLE sandbox.YURBASOV_KHOVRINO_MAXIMA
  2. -- Вход:  Ховрино
  3. -- Выход: Любой
  4.  
  5. -- VALUES 209: 2,2,209,Ховрино,Khovrino
  6.  
  7. -- за последние два месяца( с 16 мая по 15 июля) фиксировался на Ховрино >12 раз в будни и >6 раз в выходные
  8. AS
  9. WITH base1 AS (
  10.   SELECT hash_msisdn, fdt, startstid,
  11.   from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') AS d,
  12.   DATEDIFF(fdt, from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd')) tm,
  13.   weekofyear(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss')) wy,
  14.   PMOD(DATEDIFF(from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd'), '2012-01-01'), 7) ww
  15.   FROM maxima_data.metro m
  16.   WHERE DAY <= '2018-07-16' AND startstid IN (209) AND
  17.    (
  18.    from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') >= '2018-05-16'
  19.    AND from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') <= '2018-07-15'
  20.    )
  21. ), uniq1 AS (
  22.   SELECT '0' AS r, 'A' AS g, hash_msisdn, startstid AS fromst, 1 AS total, d, wy, MIN(ww) AS min_ww, MAX(tm) AS max_tm, MIN(tm) AS min_tm
  23.   FROM base1 WHERE ww IN (0,1,2,3,4,5,6) AND startstid IN (209) GROUP BY hash_msisdn, startstid, d, wy
  24. ), aggg1 AS (
  25.   SELECT r, g, hash_msisdn, fromst, SUM(total) AS cnt, MAX(d) AS max_d, MIN(d) AS min_d, MAX(max_tm) AS max_tm, MIN(min_tm) AS min_tm FROM uniq1 WHERE r = '0' GROUP BY hash_msisdn, fromst, r, g HAVING SUM(total) >= 6
  26. ), res1 AS (
  27.   SELECT a.cnt AS fromcnt, a.r, a.g, a.hash_msisdn, a.fromst, a.min_d, a.max_d, a.max_tm, a.min_tm FROM aggg1 a
  28. ), proto AS (
  29.   SELECT res1.fromcnt AS tocnt, res1.min_d AS min_d, res1.max_d AS max_d,
  30.   res1.hash_msisdn, res1.r AS r, res1.g AS g, res1.fromst AS a, '-' AS b FROM res1
  31. ), raw AS
  32. (
  33.   SELECT hash_msisdn, MAX(tocnt) AS tocnt, MIN(min_d) AS min_d, MAX(max_d) AS max_d,
  34.   MAX(CONCAT(lpad(tocnt, 3, "0"), ' ', r, ' ', g, ' ', lpad(a, 3, "0"), ' ', lpad(b, 3, "0"))) AS info,
  35.   substr(MAX(CONCAT(lpad(tocnt, 3, "0"), ' ', r, ' ', g, ' ', lpad(a, 3, "0"), ' ', lpad(b, 3, "0"))), 5, 1) AS r,
  36.   substr(MAX(CONCAT(lpad(tocnt, 3, "0"), ' ', r, ' ', g, ' ', lpad(a, 3, "0"), ' ', lpad(b, 3, "0"))), 7, 1) AS g,
  37.   CAST (substr(MAX(CONCAT(lpad(tocnt, 3, "0"), ' ', r, ' ', g, ' ', lpad(a, 3, "0"), ' ', lpad(b, 3, "0"))), 9, 3) AS INT) AS a,
  38.   CAST (substr(MAX(CONCAT(lpad(tocnt, 3, "0"), ' ', r, ' ', g, ' ', lpad(a, 3, "0"), ' ', lpad(b, 3, "0"))), 13, 3) AS INT) AS b
  39.   FROM proto GROUP BY hash_msisdn
  40. ), done AS (
  41.   SELECT raw.tocnt, raw.min_d, raw.max_d, raw.hash_msisdn, raw.r, raw.g, concat(m.station_name_rus, '-', raw.a) AS a, raw.b FROM raw
  42.     LEFT JOIN sandbox.line_station_codes_new m
  43.     ON raw.a = m.station_id
  44. ), res AS (
  45.  SELECT s.phone, done.tocnt, done.min_d, done.max_d, done.r, done.g, done.a, done.b, done.hash_msisdn FROM done JOIN secret.phones s ON done.hash_msisdn = regexp_replace(s.phone_hash, '^[0]*', '')
  46.  ) SELECT phone, MAX(tocnt) AS cnt, MAX(r) AS r, MAX(g) AS g, MAX(a) AS a, MAX(b) AS b, MAX(hash_msisdn) AS msisdn, MIN(min_d) AS min_d, MAX(max_d) AS max_d FROM res GROUP BY phone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement