GuestRT

metro_rama

Aug 28th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.04 KB | None | 0 0
  1. CREATE TABLE sandbox.yurbasov_metro_rama
  2. AS
  3. WITH base1 AS (
  4.   SELECT hash_msisdn, fdt, startstid, DAY FROM maxima_data.metro m
  5.     -- WHERE DAY >= '2018-05-01' AND DAY <= '2018-08-28' AND startstid IN (208)
  6.     WHERE DAY >= '2018-07-01' AND DAY <= '2018-08-28' AND startstid IN (208)
  7. ), uniq1 AS (
  8.   SELECT '1A' AS tags, hash_msisdn, startstid, 1 AS total FROM base1 GROUP BY hash_msisdn, startstid, DAY
  9. ), pre1 AS (
  10.   SELECT SUM(total) AS cnt, tags, hash_msisdn, startstid AS a FROM uniq1 GROUP BY tags, hash_msisdn, startstid
  11. ), raw1 AS (
  12.   SELECT cnt, tags, hash_msisdn, a FROM pre1 WHERE cnt >= 15 -- 10
  13. ), done1 AS (
  14.   SELECT cnt, hash_msisdn, concat(m.station_name_rus, '-', a) AS a FROM raw1
  15.     LEFT JOIN sandbox.line_station_codes_new m
  16.     ON raw1.a = m.station_id
  17. ), res1 AS (
  18.   SELECT s.phone, done1.cnt, done1.a, done1.hash_msisdn FROM done1
  19.     JOIN secret.phones s ON done1.hash_msisdn = regexp_replace(s.phone_hash, '^[0]*', '')
  20.  ) SELECT phone, MAX(cnt) AS cnt, MAX(a) AS a, MAX(hash_msisdn) AS msisdn FROM res1 GROUP BY phone
Advertisement
Add Comment
Please, Sign In to add comment