Advertisement
Guest User

Untitled

a guest
Jan 13th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH params AS (
  2.     SELECT
  3.      $P!{reportId} AS report_id,
  4.      5             AS OFFSET
  5. ), geo_params AS (
  6.     SELECT
  7.       CASE WHEN params.report_id = 0 THEN 2208161 ELSE g.id END AS geo_tag_id,
  8.       CASE WHEN params.report_id = 0 THEN 0 ELSE g.level END    AS level
  9.     FROM emrg_housing_reports r
  10.       RIGHT JOIN params ON  params.report_id = r.id
  11.       LEFT JOIN geo_tags g ON g.id = r.geo_tag_id
  12. ), rf AS (
  13.     SELECT
  14.       0            AS pos,
  15.       'rf' :: TEXT AS type,
  16.       g.id,
  17.       g.name,
  18.       g.parent_geo_tag_id
  19.     FROM geo_tags g
  20.     WHERE level = 0
  21. ), fo AS (
  22.     SELECT
  23.       row_number() OVER (
  24.         ORDER BY FIELD(g.id, 2208161, 2220462, 2216072, 2209857,
  25.                        2214157, 2238752, 2272232, 2208162,
  26.                        2213473)) * 10000000 AS pos,
  27.       'fo' :: TEXT                          AS type,
  28.       g.id,
  29.       g.name,
  30.       g.parent_geo_tag_id
  31.     FROM geo_tags g
  32.     WHERE g.is_archive = 0 AND level = 1
  33. ), sf AS (
  34.     SELECT
  35.       row_number() OVER (
  36.         ORDER BY fo.pos, g.name) * 100000 + fo.pos AS pos,
  37.       'sf' :: TEXT                                 AS type,
  38.       g.id,
  39.       g.name,
  40.       g.parent_geo_tag_id
  41.     FROM geo_tags g
  42.       JOIN fo ON g.parent_geo_tag_id = fo.id
  43.     WHERE g.is_archive = 0
  44. ), mo AS (
  45.     SELECT
  46.       row_number() OVER (
  47.         ORDER BY sf.pos, g.name) + sf.pos AS pos,
  48.       'mo' :: TEXT                        AS type,
  49.       g.id,
  50.       g.name,
  51.       g.parent_geo_tag_id
  52.     FROM geo_tags g
  53.       JOIN sf ON g.parent_geo_tag_id = sf.id
  54.     WHERE g.is_archive = 0
  55. ), geo_agg AS (
  56.   SELECT rf.*
  57.   FROM rf
  58.     CROSS JOIN geo_params
  59.   WHERE geo_params.level = 0
  60.   UNION ALL
  61.   SELECT fo.*
  62.   FROM fo
  63.     CROSS JOIN geo_params
  64.   WHERE geo_params.level = 0
  65.   UNION ALL
  66.   SELECT sf.*
  67.   FROM sf
  68.     CROSS JOIN geo_params
  69.   WHERE geo_params.level = 0 OR geo_params.geo_tag_id = sf.id
  70.   UNION ALL
  71.   SELECT mo.*
  72.   FROM mo
  73.     CROSS JOIN geo_params
  74.   WHERE
  75.     NOT (geo_params.geo_tag_id = ANY (ARRAY [2280999, 2276347, 2399515]))
  76.     AND (
  77.       (geo_params.level = 2 AND geo_params.geo_tag_id = mo.parent_geo_tag_id) OR
  78.       (geo_params.level = 3 AND geo_params.geo_tag_id = mo.id)
  79.     )
  80.   ORDER BY pos
  81. ), geo_num AS (
  82.     SELECT
  83.       row_number() OVER () + params.offset idx,
  84.       geo_agg.*
  85.     FROM geo_agg
  86.       CROSS JOIN params
  87. ), fo_rows AS (
  88.     SELECT
  89.       2208161                                                         AS id,
  90.       array_to_string(array_agg(idx) FILTER (WHERE type = 'fo'), ',') AS rows
  91.     FROM geo_num g
  92.     WHERE type = 'fo'
  93.     GROUP BY type
  94. ), sum_rows AS (
  95.     SELECT
  96.       DISTINCT ON (parent_geo_tag_id)
  97.       parent_geo_tag_id AS                           id,
  98.       min(idx) OVER (PARTITION BY parent_geo_tag_id) sum_from,
  99.       max(idx) OVER (PARTITION BY parent_geo_tag_id) sum_to
  100.     FROM geo_num
  101.     WHERE NOT (type = ANY (ARRAY ['rf', 'fo']))
  102. ), geo_final AS (
  103.     SELECT
  104.       geo_num.*,
  105.       fo_rows.rows,
  106.       sum_rows.sum_from,
  107.       sum_rows.sum_to
  108.     FROM geo_num
  109.       LEFT JOIN fo_rows ON geo_num.id = fo_rows.id
  110.       LEFT JOIN sum_rows ON geo_num.id = sum_rows.id
  111. ), filtered_reports_rf_case AS (
  112.     SELECT
  113.       DISTINCT ON(geo_tag_id) id,
  114.       geo_tag_id
  115.     FROM emrg_housing_reports
  116.     WHERE deleted_at IS NULL
  117.     ORDER BY geo_tag_id, created_at DESC
  118. ), filtered_report_mo_rf_case AS (
  119.     SELECT
  120.       r.geo_tag_id,
  121.       coalesce(sum(city_mkd_count),0)                                         AS f3,
  122.       coalesce(sum(city_blocked_mkd_count),0)                                 AS f4,
  123.       coalesce(sum(city_individual_mkd_count),0)                              AS f5,
  124.       coalesce(sum(city_special_fond_mkd_count),0)                            AS f6,
  125.       coalesce(sum(village_mkd_count),0)                                      AS f7,
  126.       coalesce(sum(village_blocked_mkd_count),0)                              AS f8,
  127.       coalesce(sum(village_individual_mkd_count),0)                           AS f9,
  128.       coalesce(sum(village_special_fond_mkd_count),0)                         AS f10,
  129.       coalesce(sum(city_area_residental)/1000,0)                              AS f11,
  130.       coalesce(sum(city_blocked_area_residental)/1000,0)                      AS f12,
  131.       coalesce(sum(city_individual_area_residental)/1000,0)                   AS f13,
  132.       coalesce(sum(city_special_fond_area_residental)/1000,0)                 AS f14,
  133.       coalesce(sum(village_area_residental)/1000,0)                           AS f15,
  134.       coalesce(sum(village_blocked_area_residental)/1000,0)                   AS f16,
  135.       coalesce(sum(village_individual_area_residental)/1000,0)                AS f17,
  136.       coalesce(sum(village_special_fond_area_residental)/1000,0)              AS f18,
  137.       coalesce(sum(city_area_residental_resettlement)/1000,0)                 AS f19,
  138.       coalesce(sum(city_blocked_area_residental_resettlement)/1000,0)         AS f20,
  139.       coalesce(sum(city_individual_area_residental_resettlement)/1000,0)      AS f21,
  140.       coalesce(sum(city_special_fond_area_residental_resettlement)/1000,0)    AS f22,
  141.       coalesce(sum(village_area_residental_resettlement)/1000,0)              AS f23,
  142.       coalesce(sum(village_blocked_area_residental_resettlement)/1000,0)      AS f24,
  143.       coalesce(sum(village_individual_area_residental_resettlement)/1000,0)   AS f25,
  144.       coalesce(sum(village_special_fond_area_residental_resettlement)/1000,0) AS f26,
  145.       coalesce(sum(city_people_count),0)                                      AS f27,
  146.       coalesce(sum(city_blocked_people_count),0)                              AS f28,
  147.       coalesce(sum(city_individual_people_count),0)                           AS f29,
  148.       coalesce(sum(city_special_fond_people_count),0)                         AS f30,
  149.       coalesce(sum(village_people_count),0)                                   AS f31,
  150.       coalesce(sum(village_blocked_people_count),0)                           AS f32,
  151.       coalesce(sum(village_individual_people_count),0)                        AS f33,
  152.       coalesce(sum(village_special_fond_people_count),0)                      AS f34,
  153.       coalesce(sum(city_people_count_resettled),0)                            AS f35,
  154.       coalesce(sum(city_blocked_people_count_resettled),0)                    AS f36,
  155.       coalesce(sum(city_individual_people_count_resettled),0)                 AS f37,
  156.       coalesce(sum(city_special_fond_people_count_resettled),0)               AS f38,
  157.       coalesce(sum(village_people_count_resettled),0)                         AS f39,
  158.       coalesce(sum(village_blocked_people_count_resettled),0)                 AS f40,
  159.       coalesce(sum(village_individual_people_count_resettled),0)              AS f41,
  160.       coalesce(sum(village_special_fond_people_count_resettled),0)            AS f42,
  161.       coalesce(sum(city_area_residental_resettled)/1000,0)                    AS f43,
  162.       coalesce(sum(city_blocked_area_residental_resettled)/1000,0)            AS f44,
  163.       coalesce(sum(city_individual_area_residental_resettled)/1000,0)         AS f45,
  164.       coalesce(sum(city_special_fond_area_residental_resettled)/1000,0)       AS f46,
  165.       coalesce(sum(village_area_residental_resettled)/1000,0)                 AS f47,
  166.       coalesce(sum(village_blocked_area_residental_resettled)/1000,0)         AS f48,
  167.       coalesce(sum(village_individual_area_residental_resettled)/1000,0)      AS f49,
  168.       coalesce(sum(village_special_fond_area_residental_resettled)/1000,0)    AS f50,
  169.       coalesce(sum(city_area_residental_demolished)/1000,0)                   AS f51,
  170.       coalesce(sum(city_blocked_area_residental_demolished)/1000,0)           AS f52,
  171.       coalesce(sum(city_individual_area_residental_demolished)/1000,0)        AS f53,
  172.       coalesce(sum(city_special_fond_area_residental_demolished)/1000,0)      AS f54,
  173.       coalesce(sum(village_area_residental_demolished)/1000,0)                AS f55,
  174.       coalesce(sum(village_blocked_area_residental_demolished)/1000,0)        AS f56,
  175.       coalesce(sum(village_individual_area_residental_demolished)/1000,0)     AS f57,
  176.       coalesce(sum(village_special_fond_area_residental_demolished)/1000,0)   AS f58,
  177.       coalesce(sum(mkd_in_reg_programm_count),0)                              AS f59,
  178.       coalesce(sum(city_mkd_in_reg_programm_count),0)                         AS f60,
  179.       coalesce(sum(area_residental_in_reloc_reg_prog)/1000,0)                 AS f61,
  180.       coalesce(sum(city_area_residental_in_reloc_reg_prog)/1000,0)            AS f62,
  181.       coalesce(sum(people_resettled_count),0)                                 AS f63,
  182.       coalesce(sum(city_people_resettled_count),0)                            AS f64
  183.     FROM emrg_housing_reports_mo mo
  184.       JOIN filtered_reports_rf_case r ON r.id = mo.emrg_housing_reports_id
  185.     GROUP BY r.geo_tag_id
  186. ), rf_case AS (
  187.     SELECT
  188.       geo_final.pos,
  189.       geo_final.name                                              AS f2,
  190.       geo_final.rows,
  191.       geo_final.sum_from,
  192.       geo_final.sum_to,
  193.       mo.*
  194.     FROM geo_final
  195.     LEFT JOIN filtered_report_mo_rf_case mo ON geo_final.id = mo.geo_tag_id
  196.     ORDER BY geo_final.pos
  197. ), sf_case AS (
  198.     SELECT
  199.       geo_final.pos,
  200.       geo_final.name                                                     AS f2,
  201.       geo_final.rows,
  202.       geo_final.sum_from,
  203.       geo_final.sum_to,
  204.       0 :: INTEGER,
  205.       coalesce(city_mkd_count,0)                                         AS f3,
  206.       coalesce(city_blocked_mkd_count,0)                                 AS f4,
  207.       coalesce(city_individual_mkd_count,0)                              AS f5,
  208.       coalesce(city_special_fond_mkd_count,0)                            AS f6,
  209.       coalesce(village_mkd_count,0)                                      AS f7,
  210.       coalesce(village_blocked_mkd_count,0)                              AS f8,
  211.       coalesce(village_individual_mkd_count,0)                           AS f9,
  212.       coalesce(village_special_fond_mkd_count,0)                         AS f10,
  213.       coalesce(city_area_residental/1000,0)                              AS f11,
  214.       coalesce(city_blocked_area_residental/1000,0)                      AS f12,
  215.       coalesce(city_individual_area_residental/1000,0)                   AS f13,
  216.       coalesce(city_special_fond_area_residental/1000,0)                 AS f14,
  217.       coalesce(village_area_residental/1000,0)                           AS f15,
  218.       coalesce(village_blocked_area_residental/1000,0)                   AS f16,
  219.       coalesce(village_individual_area_residental/1000,0)                AS f17,
  220.       coalesce(village_special_fond_area_residental/1000,0)              AS f18,
  221.       coalesce(city_area_residental_resettlement/1000,0)                 AS f19,
  222.       coalesce(city_blocked_area_residental_resettlement/1000,0)         AS f20,
  223.       coalesce(city_individual_area_residental_resettlement/1000,0)      AS f21,
  224.       coalesce(city_special_fond_area_residental_resettlement/1000,0)    AS f22,
  225.       coalesce(village_area_residental_resettlement/1000,0)              AS f23,
  226.       coalesce(village_blocked_area_residental_resettlement/1000,0)      AS f24,
  227.       coalesce(village_individual_area_residental_resettlement/1000,0)   AS f25,
  228.       coalesce(village_special_fond_area_residental_resettlement/1000,0) AS f26,
  229.       coalesce(city_people_count,0)                                      AS f27,
  230.       coalesce(city_blocked_people_count,0)                              AS f28,
  231.       coalesce(city_individual_people_count,0)                           AS f29,
  232.       coalesce(city_special_fond_people_count,0)                         AS f30,
  233.       coalesce(village_people_count,0)                                   AS f31,
  234.       coalesce(village_blocked_people_count,0)                           AS f32,
  235.       coalesce(village_individual_people_count,0)                        AS f33,
  236.       coalesce(village_special_fond_people_count,0)                      AS f34,
  237.       coalesce(city_people_count_resettled,0)                            AS f35,
  238.       coalesce(city_blocked_people_count_resettled,0)                    AS f36,
  239.       coalesce(city_individual_people_count_resettled,0)                 AS f37,
  240.       coalesce(city_special_fond_people_count_resettled,0)               AS f38,
  241.       coalesce(village_people_count_resettled,0)                         AS f39,
  242.       coalesce(village_blocked_people_count_resettled,0)                 AS f40,
  243.       coalesce(village_individual_people_count_resettled,0)              AS f41,
  244.       coalesce(village_special_fond_people_count_resettled,0)            AS f42,
  245.       coalesce(city_area_residental_resettled/1000,0)                    AS f43,
  246.       coalesce(city_blocked_area_residental_resettled/1000,0)            AS f44,
  247.       coalesce(city_individual_area_residental_resettled/1000,0)         AS f45,
  248.       coalesce(city_special_fond_area_residental_resettled/1000,0)       AS f46,
  249.       coalesce(village_area_residental_resettled/1000,0)                 AS f47,
  250.       coalesce(village_blocked_area_residental_resettled/1000,0)         AS f48,
  251.       coalesce(village_individual_area_residental_resettled/1000,0)      AS f49,
  252.       coalesce(village_special_fond_area_residental_resettled/1000,0)    AS f50,
  253.       coalesce(city_area_residental_demolished/1000,0)                   AS f51,
  254.       coalesce(city_blocked_area_residental_demolished/1000,0)           AS f52,
  255.       coalesce(city_individual_area_residental_demolished/1000,0)        AS f53,
  256.       coalesce(city_special_fond_area_residental_demolished/1000,0)      AS f54,
  257.       coalesce(village_area_residental_demolished/1000,0)                AS f55,
  258.       coalesce(village_blocked_area_residental_demolished/1000,0)        AS f56,
  259.       coalesce(village_individual_area_residental_demolished/1000,0)     AS f57,
  260.       coalesce(village_special_fond_area_residental_demolished/1000,0)   AS f58,
  261.       coalesce(mkd_in_reg_programm_count,0)                              AS f59,
  262.       coalesce(city_mkd_in_reg_programm_count,0)                         AS f60,
  263.       coalesce(area_residental_in_reloc_reg_prog/1000,0)                 AS f61,
  264.       coalesce(city_area_residental_in_reloc_reg_prog/1000,0)            AS f62,
  265.       coalesce(people_resettled_count,0)                                 AS f63,
  266.       coalesce(city_people_resettled_count,0)                            AS f64
  267.     FROM geo_final
  268.       CROSS JOIN params
  269.       LEFT JOIN emrg_housing_reports_mo dt ON dt.geo_tag_id = geo_final.id AND dt.emrg_housing_reports_id = params.report_id
  270.     ORDER BY geo_final.pos
  271. ), union_data AS (
  272.   SELECT
  273.     sf_case.*
  274.   FROM
  275.     sf_case
  276.     CROSS JOIN geo_params
  277.   WHERE geo_params.geo_tag_id <> 2208161
  278.   UNION
  279.   SELECT
  280.     rf_case.*
  281.   FROM rf_case
  282.     CROSS JOIN geo_params
  283.   WHERE geo_params.geo_tag_id = 2208161
  284. ) SELECT * FROM union_data ORDER BY pos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement