Guest User

Untitled

a guest
Jan 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. create local temp table sample_canonicals on commit preserve rows as
  2. (
  3. with canonicals as
  4. (
  5. select c.id as canonical_place_id,
  6. count(distinct place_id) as places
  7. from canonical_places c inner join place_mappings m on c.id = m.canonical_place_id
  8. where enabled
  9. group by c.id
  10. order by random()
  11. )
  12. select canonical_place_id
  13. from canonicals
  14. --where places > 10
  15. --limit 1000
  16. )
  17. ;
  18.  
  19. create local temp table relevant_info on commit preserve rows as
  20. (
  21. --with sample_canonicals as (select '33717'::int as canonical_place_id)
  22. select c.canonical_place_id,
  23. m.place_id,
  24. m.owner_verified,
  25. s.priority,
  26. m.updated_at,
  27. row_number() over (partition by canonical_place_id
  28. order by m.owner_verified desc,
  29. s.priority asc,
  30. m.updated_at asc,
  31. m.place_id asc) as layer_order
  32. from sample_canonicals c inner join place_mappings m using (canonical_place_id)
  33. inner join places p on m.place_id = p.id
  34. inner join sources s on p.source_id = s.id
  35. order by c.canonical_place_id,
  36. m.owner_verified desc,
  37. s.priority asc,
  38. m.updated_at asc,
  39. m.place_id asc
  40. )
  41. ;
  42.  
  43. create local temp table details on commit preserve rows as
  44. (
  45. select i.*,
  46. p.name,
  47. regexp_replace(p.description, ',|\n', '', 'g') as description,
  48. p.subtitle,
  49. p.alt_names,
  50. d.permanently_closed,
  51. p.enabled
  52. from relevant_info i inner join places p on i.place_id = p.id
  53. left join place_details d using (place_id)
  54. order by i.canonical_place_id, i.layer_order
  55. )
  56. ;
  57.  
  58. create local temp table details_enabled on commit preserve rows as
  59. (
  60. select * from details where enabled is true
  61. )
  62. ;
  63.  
  64. create local temp table top_values on commit preserve rows as
  65. (
  66. select canonical_place_id,
  67. first_value(name) over (partition by canonical_place_id order by layer_order asc, name is null, ts) as name,
  68. first_value(description) over (partition by canonical_place_id order by layer_order asc, description is null, ts) as description,
  69. first_value(subtitle) over (partition by canonical_place_id order by layer_order asc, subtitle is null, ts) as subtitle,
  70. first_value(alt_names) over (partition by canonical_place_id order by layer_order asc, alt_names is null, ts) as alt_names
  71. from details
  72. order by canonical_place_id
  73. )
  74. ;
Add Comment
Please, Sign In to add comment