Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. -- 2329523
  2. --SELECT * FROM geo_tags WHERE name like '%Тюме%' and level = 2;
  3.  
  4.  
  5. WITH cte_sf_houses AS (
  6. SELECT
  7. h.id AS house_id,
  8. hr.id AS house_revision_id
  9. FROM houses h
  10. JOIN house_revision hr ON hr.houses_id = h.id AND hr.is_current = 1
  11. JOIN geo_tags gt ON gt.id = hr.geo_tag_id
  12. JOIN geo_tags gp ON gt.lft BETWEEN gp.lft AND gp.rgt
  13. WHERE
  14. h.deleted_at IS NULL
  15. AND hr.is_alarm = TRUE
  16. AND gp.id = 2329523
  17. ), cte_demolition_filter AS (
  18. SELECT
  19. csh.house_id,
  20. csh.house_revision_id
  21. FROM cte_sf_houses csh
  22. JOIN epd_house_accidents ha ON ha.house_revision_id = csh.house_revision_id
  23. JOIN refs alarm_reason_ref ON alarm_reason_ref.id = ha.alarm_reason_id
  24. WHERE
  25. (
  26. ha.actual_demolition_date IS NULL
  27. OR ha.actual_demolition_date >= '2019-01-01'
  28. )
  29. AND
  30. (
  31. ha.actual_settlement_completion_date IS NULL
  32. OR ha.actual_settlement_completion_date >= '2019-01-01'
  33. )
  34. AND ha.alarm_document_date < '2017-01-01'
  35. AND alarm_reason_ref.code = '0601' -- Физический износ
  36. ), cte_house_type_filter AS (
  37. SELECT
  38. cdf.house_id,
  39. cdf.house_revision_id
  40. FROM cte_demolition_filter cdf
  41. JOIN epd_house_passports hp ON hp.house_revision_id = cdf.house_revision_id
  42. JOIN refs house_type_ref ON house_type_ref.id = hp.house_type_id
  43. WHERE house_type_ref.code = '0501' -- Многоквартирный
  44. ), cte_houses_to_insert AS ( -- houses_to_insert
  45. SELECT
  46. DISTINCT ON (chtf.house_id)
  47. chtf.house_id,
  48. chtf.house_revision_id
  49. FROM cte_house_type_filter chtf
  50. JOIN house_quarters_residents_snapshots hqrs ON hqrs.house_revision_id = chtf.house_revision_id AND hqrs.is_main
  51. JOIN house_quarters hq ON hq.snapshot_id = hqrs.id
  52. LEFT JOIN refs resettlement_status_ref ON resettlement_status_ref.id = hq.resettlement_status_id
  53. WHERE
  54. hq.deleted_at IS NUlL
  55. AND resettlement_status_ref.code <> 'empty'
  56. AND (resettlement_status_ref.code <> 'resettled' OR hq.actual_settlement_completion_date >= '2019-01-01')
  57. ), cte_house_quarters AS (
  58. SELECT
  59. chti.house_id,
  60. hq.id AS hq_id,
  61. hq.square,
  62. hq.residents_count,
  63. hr.resettlement_program_id,
  64. program_type_ref.code AS program_type
  65. FROM cte_houses_to_insert chti
  66. JOIN house_quarters_residents_snapshots hqrs ON hqrs.house_revision_id = chti.house_revision_id AND hqrs.is_main
  67. JOIN house_quarters hq ON hq.snapshot_id = hqrs.id
  68. LEFT JOIN refs resettlement_status_ref ON resettlement_status_ref.id = hq.resettlement_status_id
  69. LEFT JOIN house_resettlements hr ON hr.quarters_id = hq.id
  70. LEFT JOIN reloc_programs_name_and_requisites rpnar ON rpnar.id = hr.resettlement_program_id
  71. LEFT JOIN refs program_type_ref ON program_type_ref.id = rpnar.program_type_id
  72. WHERE
  73. hq.deleted_at IS NULL
  74. AND hr.deleted_at IS NULL
  75. AND resettlement_status_ref.code <> 'empty'
  76. AND (resettlement_status_ref.code <> 'resettled' OR hq.actual_settlement_completion_date >= '2019-01-01')
  77. ) --SELECT * FROM cte_house_quarters WHERE house_id IN (9092341, 6583977);
  78. , cte_for_limits_filter AS (
  79. SELECT
  80. DISTINCT ON (chq.hq_id)
  81. chq.house_id,
  82. chq.hq_id,
  83. chq.square,
  84. chq.residents_count
  85. FROM cte_house_quarters chq
  86. WHERE chq.resettlement_program_id IS NULL
  87. ), sq AS (
  88. SELECT
  89. cflf.house_id,
  90. sum(cflf.square) AS square_for_limits
  91. -- cflf.residents_count AS residents_for_limits,
  92. -- cflf.hq_id AS quarters_for_limits
  93. FROM cte_for_limits_filter cflf
  94. GROUP BY cflf.house_id
  95. )
  96. SELECT
  97. -- sum(square_for_limits)
  98. *
  99. FROM sq-- WHERE house_id = 8646222
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement