Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TRUNCATE service.service_static_url RESTART IDENTITY;
  2.  
  3. WITH
  4. all_divesite AS (
  5.   SELECT
  6.     divesite.id,
  7.     divesite.country_id,
  8.     divesite.name,
  9.     COALESCE(divesite.region_id, 0) as region_id
  10.   FROM ds.ds_divesite divesite
  11. ),
  12. excluded_divesite AS (
  13.   SELECT
  14.     t.name,
  15.     t.country_id,
  16.     max(t.region_id) as region_id
  17.   FROM all_divesite t
  18.  
  19.   GROUP BY t.name, t.country_id
  20. ),
  21. select_divesite AS (
  22.   SELECT
  23.     ad.*
  24.   FROM excluded_divesite ed
  25.     JOIN all_divesite ad ON ad.region_id = ed.region_id
  26.       AND ad.country_id = ed.country_id
  27.       AND ad.name = ed.name
  28. ),
  29. divesite_url AS (
  30.   SELECT
  31.     t.human_url,
  32.     'divesite/view?id=' || t.id as default_url
  33.   FROM (
  34.     SELECT
  35.       human_url,
  36.       max(id) as id
  37.     FROM (
  38.       SELECT
  39.           'discover/' || lower(replace(country.name, ' ', '-')) || '/'  || lower(replace(divesite.name, ' ', '-')) as human_url,
  40.           divesite.id
  41.         FROM select_divesite divesite
  42.         JOIN dict.dict_country country ON country.id = divesite.country_id
  43.     ) t
  44.     GROUP BY human_url
  45.   ) t
  46. )
  47.  
  48. SELECT * FROM divesite_url
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement