Advertisement
GuestRT

Metro-Auto-Check-3

Apr 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.81 KB | None | 0 0
  1. CREATE TABLE sandbox.yurbasov_metro_auto_check_3
  2. AS
  3. WITH task1 AS (
  4.   SELECT t.v, s.phone_hash FROM sandbox.yurbasov_task_auto_check t
  5.   LEFT JOIN secret.phones s ON t.v = s.phone
  6. ), chos1 AS (
  7.   SELECT v, regexp_replace(phone_hash, '^[0]*', '') AS short_hash FROM task1 WHERE phone_hash IS NOT NULL
  8. ), base1 AS (
  9.   SELECT v, hash_msisdn, fdt, startstid, stopstid,
  10.   from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') AS d,
  11.   DATEDIFF(fdt, from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd')) tm,
  12.   weekofyear(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss')) wy,
  13.   PMOD(DATEDIFF(from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd'), '2012-01-01'), 7) ww
  14.   FROM chos1 t JOIN maxima_data.metro m ON t.short_hash = m.hash_msisdn
  15.   WHERE DAY <= '2018-03-28' AND
  16.    (
  17.    from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') >= '2017-09-01'
  18.    AND from_unixtime(unix_timestamp(fdt, 'dd.MM.yyyy HH:mm:ss'),'yyyy-MM-dd') <= '2018-02-28'
  19.    )
  20. ), uniq1 AS (
  21.   SELECT '0' AS r, 'A' AS g, v, hash_msisdn, startstid AS fromst, stopstid AS tost, 1 AS total, d, wy, MIN(ww) AS min_ww, MAX(tm) AS max_tm, MIN(tm) AS min_tm
  22.   FROM base1 WHERE ww IN (0,1,2,3,4,5,6) GROUP BY v, hash_msisdn, startstid, stopstid, d, wy
  23. ), aggg1 AS (
  24.   SELECT r, g, v, hash_msisdn, fromst, tost, 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 v, hash_msisdn, fromst, tost, r, g HAVING SUM(total) >= 1
  25. ), res1 AS (
  26.   SELECT a.cnt AS fromcnt, a.r, a.g, a.v, a.hash_msisdn, a.fromst, a.tost, a.min_d, a.max_d, a.max_tm, a.min_tm FROM aggg1 a
  27. ), anti1 AS (
  28.   SELECT task1.v, res1.v AS z FROM task1 LEFT JOIN res1 ON task1.v = res1.v
  29. ), proto AS (
  30.   SELECT res1.fromcnt AS tocnt, res1.min_d AS min_d, res1.max_d AS max_d,
  31.   res1.v, res1.hash_msisdn, res1.r AS r, res1.g AS g, res1.fromst AS a, res1.tost AS b FROM res1
  32. ), raw AS (
  33.   SELECT v, 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 v, hash_msisdn
  40. ), enrich AS (
  41.   SELECT raw.tocnt, raw.min_d, raw.max_d, raw.v, raw.hash_msisdn,
  42.  raw.r, raw.g, concat(m.station_name_rus, '-', raw.a) AS a, raw.b FROM raw
  43.     LEFT JOIN sandbox.line_station_codes_new m
  44.     ON raw.a = m.station_id
  45. ), done AS (
  46.   SELECT enrich.tocnt, enrich.min_d, enrich.max_d, enrich.v, enrich.hash_msisdn,
  47.  enrich.r, enrich.g, a, concat(m.station_name_rus, '-', enrich.b) AS b FROM enrich
  48.     LEFT JOIN sandbox.line_station_codes_new m
  49.     ON enrich.b = m.station_id
  50. ), res AS (
  51.  SELECT s.phone, done.tocnt, done.min_d, done.max_d, done.r, done.g, done.a, done.b, done.v, done.hash_msisdn FROM done JOIN secret.phones s ON done.hash_msisdn = regexp_replace(s.phone_hash, '^[0]*', '')
  52.  ), anti AS (
  53.   SELECT a.v AS phone, '-' AS cnt, '-' AS r, '-' AS g, '-' AS a, '-'  AS b, '-' AS v, '-' AS msisdn, '-' AS min_d, '-' AS max_d FROM anti1 a WHERE a.z IS NULL
  54. ) SELECT concat('', phone) AS phone, CONCAT('', MAX(tocnt)) AS cnt, MAX(r) AS r, MAX(g) AS g, MAX(a) AS a, MAX(b) AS b, CONCAT('', MAX(v)) AS v, MAX(hash_msisdn) AS msisdn, MIN(min_d) AS min_d, MAX(max_d) AS max_d FROM res GROUP BY phone
  55. UNION ALL
  56. SELECT phone, cnt, r, g, a, b, v, msisdn, min_d, max_d FROM anti
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement