Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.16 KB | None | 0 0
  1. WITH cte_osn AS (
  2. SELECT
  3. gt.name,
  4. gt.id,
  5. gt.parent_geo_tag_id,
  6. gt.level,
  7. CASE WHEN gt.level = 1 THEN gt.id when gt.level = 2 THEN gt.parent_geo_tag_id END AS rn_parent_geo_tag_id,
  8. nullif(count(*) FILTER (WHERE cat.code = 'dmp'), 0) AS w1,
  9. nullif(count(*) FILTER (WHERE cat.code = 'oth'), 0) AS w2,
  10. nullif(count(*) FILTER (WHERE cat.code = 'orc'), 0) AS w3,
  11. nullif(count(*) FILTER (WHERE cat.code = 'lc'), 0) AS w4,
  12. nullif(count(*) FILTER (WHERE cat.code = 'qmp'), 0) AS w5,
  13. nullif(count(*) FILTER (WHERE cat.code = 'uir'), 0) AS w6,
  14. nullif(count(*) FILTER (WHERE cat.code = 'mo'), 0) AS w7,
  15. nullif(count(*) FILTER (WHERE cat.code = 'wwp'), 0) AS w8,
  16. nullif(count(*) FILTER (WHERE cat.code = 'tko'), 0) AS w9,
  17. nullif(count(*) FILTER (WHERE cat.code = 'smo'), 0) AS w10,
  18. nullif(count(*) FILTER (WHERE cat.code = 'qmo'), 0) AS w11,
  19. nullif(count(*) FILTER (WHERE cat.code = 'vmo'), 0) AS w12,
  20. nullif(count(*) FILTER (WHERE cat.code = 'col'), 0) AS w13,
  21. count(*) AS w0
  22. FROM geo_tags gt
  23. LEFT JOIN (
  24. SELECT h.* FROM handlings h
  25. JOIN statuses s ON s.id = h.status_id
  26. WHERE s.code <> 'handling.rejected'
  27. AND h.created_date BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  28. ) h ON gt.id = h.region_id
  29. LEFT JOIN refs cat ON cat.id = h.category_id
  30. WHERE gt.level IN (2,1) --and created_date is not null
  31. AND (gt.parent_geo_tag_id = ANY (ARRAY[:geoTagIds] :: INT[]) OR gt.id = ANY (ARRAY[:geoTagIds] :: INT[])) -- округ или список субъектов
  32. GROUP BY gt.name, gt.level, gt.id, gt.parent_geo_tag_id
  33. ), cte_treated AS (
  34. SELECT
  35. gt2.geo_tag_id AS id,
  36. count(gt2.geo_tag_name) AS treated --Рассмотрено
  37. FROM (
  38. SELECT
  39. h.id,
  40. h.status_id,
  41. h.surname,
  42. h.name,
  43. g.name AS geo_tag_name,
  44. g.level AS geo_tag_level,
  45. g.parent_geo_tag_id AS parent_geo_tag_id,
  46. g.id AS geo_tag_id
  47. FROM handlings h
  48. JOIN handlings_status_changes_relations scr ON h.id = scr.handling_id
  49. JOIN status_changes sc ON scr.statuschange_id = sc.id AND sc.is_successfully
  50. LEFT JOIN geo_tags g ON h.region_id = g.id
  51. JOIN statuses s ON s.id = h.status_id
  52. JOIN statuses s1 ON s1.id = sc.to_status_id
  53. WHERE s.code = 'handling.answered'
  54. AND h.created_date BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  55. AND h.id IN (
  56. SELECT
  57. h.id
  58. FROM handlings h
  59. JOIN handling_replies hr ON hr.handling_id = h.id
  60. GROUP BY h.id
  61. HAVING max(hr.created) BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  62. )
  63. GROUP BY h.id, g.id
  64. HAVING NOT bool_or(s1.code = 'handling.forwarded')
  65. ) gt2
  66. GROUP BY gt2.geo_tag_level, gt2.parent_geo_tag_id, gt2.geo_tag_id
  67. ), cte_days_for_reply AS (
  68. SELECT
  69. gt2.id,
  70. round(
  71. avg(nullif(to_char(hr.created - h.created_date, 'DD'), '') :: int), 0
  72. ) AS days_for_reply --Время рассмотрения
  73. FROM handlings h
  74. JOIN (
  75. SELECT hr.created, hr.handling_id
  76. FROM handling_replies hr
  77. INNER JOIN (
  78. SELECT
  79. id, max(created) OVER (PARTITION BY handling_id) = created AS is_last_created
  80. FROM handling_replies
  81. ) t ON hr.id = t.id
  82. WHERE t.is_last_created
  83. ) hr ON h.id = hr.handling_id
  84. JOIN geo_tags gt2 ON gt2.id = h.region_id
  85. JOIN statuses s ON s.id = h.status_id
  86. JOIN refs cat ON cat.id = h.category_id
  87. WHERE s.code IN ('handling.answered', 'handling.forwarded')
  88. AND hr.created BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  89. AND cat.code <> 'uir'
  90. AND nullif(to_char(hr.created - h.created_date, 'DD'), '') :: int < 90
  91. GROUP BY gt2.id, gt2.level, gt2.parent_geo_tag_id
  92. ), cte_sc AS (
  93. SELECT
  94. sc.id, sc.to_status_id, sc.created_at, sc.is_successfully
  95. FROM status_changes sc
  96. INNER JOIN (
  97. SELECT
  98. id, max(created_at) OVER (PARTITION BY hscr.handling_id, sc.to_status_id) = sc.created_at AS is_last_created
  99. FROM status_changes sc
  100. INNER JOIN handlings_status_changes_relations hscr ON sc.id = hscr.statuschange_id
  101. ) t ON t.id = sc.id
  102. WHERE t.is_last_created
  103. ), cte_oiv AS (
  104. SELECT
  105. gt2.id, count(DISTINCT h.id) AS oiv --Перенаправлено в ОИВ
  106. FROM handlings h
  107. JOIN handlings_status_changes_relations hscr ON hscr.handling_id = h.id
  108. JOIN (
  109. SELECT
  110. sc.id, sc.to_status_id, sc.created_at, sc.is_successfully
  111. FROM status_changes sc
  112. INNER JOIN (
  113. SELECT
  114. id, max(created_at) OVER (PARTITION BY hscr.handling_id, sc.to_status_id) = sc.created_at AS is_last_created
  115. FROM status_changes sc
  116. INNER JOIN handlings_status_changes_relations hscr ON sc.id = hscr.statuschange_id
  117. ) t ON t.id = sc.id
  118. WHERE t.is_last_created
  119. ) sc ON hscr.statuschange_id = sc.id AND sc.is_successfully = 'true'
  120. JOIN statuses s ON s.id = h.status_id
  121. JOIN statuses s1 ON s1.id = sc.to_status_id
  122. LEFT JOIN geo_tags gt2 ON h.region_id = gt2.id
  123. WHERE h.created_date BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  124. AND sc.created_at BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  125. AND s1.code = 'handling.forwarded'
  126. AND h.id IN (sc.id)
  127. GROUP BY gt2.id, gt2.name
  128. ), cte_control AS (
  129. SELECT
  130. gt2.id,
  131. count(h.*) AS control --На контроле
  132. FROM handlings h
  133. LEFT JOIN geo_tags gt2 ON h.region_id = gt2.id
  134. JOIN statuses s ON s.id = h.status_id
  135. WHERE s.code = 'handling.forwarded'
  136. AND h.created_date BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  137. GROUP BY gt2.id
  138. ), cte_control_50 AS (
  139. SELECT
  140. gt2.id,
  141. count(h.*) AS control_50 --На контроле 50
  142. FROM handlings h
  143. LEFT JOIN geo_tags gt2 ON h.region_id = gt2.id
  144. JOIN statuses s ON s.id = h.status_id
  145. WHERE s.code = 'handling.forwarded'
  146. AND h.created_date BETWEEN coalesce(:dateFrom :: date, '-infinity') AND coalesce(:dateTo :: date, 'infinity')
  147. AND (now() :: date - created_date :: date > 50)
  148. GROUP BY gt2.id
  149. ), cte_it_sum AS (
  150. SELECT
  151. rank() OVER (ORDER BY w.rn_parent_geo_tag_id, w.id) AS rn,
  152. w.id,
  153. w.name,
  154. coalesce(sum(w.w0), 0) + coalesce(sum(w.w1), 0) + coalesce(sum(w.w2), 0) +
  155. coalesce(sum(w.w3), 0) + coalesce(sum(w.w4), 0) + coalesce(sum(w.w5), 0) +
  156. coalesce(sum(w.w6), 0) + coalesce(sum(w.w7), 0) + coalesce(sum(w.w8), 0) +
  157. coalesce(sum(w.w9), 0) + coalesce(sum(w.w10), 0) + coalesce(sum(w.w11), 0) +
  158. coalesce(sum(w.w12), 0) + coalesce(sum(w.w13), 0) AS summa
  159. FROM cte_osn w
  160. GROUP BY w.rn_parent_geo_tag_id, w.id, w.name
  161. ), cte_sf_data AS (
  162. SELECT
  163. rank() OVER (ORDER BY it.rn) AS rn,
  164. 1 AS total_group,
  165. pgt.id AS fo_group,
  166. gt.name AS sf_name,
  167. pgt.name AS fo_name,
  168. nullif(it.summa, 0) AS summary, --2
  169. nullif(w.w0, 0) AS in_ais, --3
  170. w.w0 * 100 / nullif(it.summa, 0) AS p4, --4
  171. w.w1, --5
  172. w.w1 * 100 / nullif(it.summa, 0) AS p6, --6
  173. w.w2, --7
  174. w.w2 * 100 / nullif(it.summa, 0) AS p8, --8
  175. w.w3, --9
  176. w.w3 * 100 / nullif(it.summa, 0) AS p10, --10
  177. w.w4, --11
  178. w.w4 * 100 / nullif(it.summa, 0) AS p12, --12
  179. w.w5, --13
  180. w.w6, --15
  181. w.w7, --17
  182. w.w8, --19
  183. w.w9, --21
  184. w.w10, --23
  185. w.w11, --25
  186. w.w12, --27
  187. w.w13, --29
  188. t.treated, --31
  189. dfr.days_for_reply, --32
  190. o.oiv, --33
  191. control, --34
  192. control_50 --35
  193. FROM cte_osn w
  194. JOIN geo_tags gt ON gt.id = w.id
  195. JOIN geo_tags pgt ON gt.lft BETWEEN pgt.lft AND pgt.rgt AND pgt.level = 1
  196. JOIN cte_it_sum it ON it.id = w.id
  197. LEFT JOIN cte_treated t ON t.id = w.id
  198. LEFT JOIN cte_days_for_reply dfr ON dfr.id = t.id
  199. LEFT JOIN cte_oiv o ON o.id = dfr.id
  200. LEFT JOIN cte_control c ON c.id = o.id
  201. LEFT JOIN cte_control_50 c50 ON c50.id = c.id
  202. WHERE w.level = 2 OR (
  203. coalesce(w1, 0) + coalesce(w2, 0) + coalesce(w3, 0) + coalesce(w4, 0) +
  204. coalesce(w5, 0) + coalesce(w6, 0) + coalesce(w7, 0) + coalesce(w8, 0) +
  205. coalesce(w9, 0) + coalesce(w10, 0) + coalesce(w11, 0) + coalesce(w12, 0) + coalesce(w13, 0) > 0
  206. )
  207. ), cte_total_by_rf AS (
  208. SELECT
  209. sum(in_ais) * 100.0 / nullif(sum(summary), 0) AS p4_rf,
  210. sum(w1) * 100.0 / nullif(sum(summary), 0) AS p6_rf,
  211. sum(w2) * 100.0 / nullif(sum(summary), 0) AS p8_rf,
  212. sum(w3) * 100.0 / nullif(sum(summary), 0) AS p10_rf,
  213. sum(w4) * 100.0 / nullif(sum(summary), 0) AS p12_rf,
  214. sum(w5) * 100.0 / nullif(sum(summary), 0) AS p14_rf,
  215. sum(w6) * 100.0 / nullif(sum(summary), 0) AS p16_rf,
  216. sum(w7) * 100.0 / nullif(sum(summary), 0) AS p18_rf,
  217. sum(w8) * 100.0 / nullif(sum(summary), 0) AS p20_rf,
  218. sum(w9) * 100.0 / nullif(sum(summary), 0) AS p22_rf,
  219. sum(w10) * 100.0 / nullif(sum(summary), 0) AS p24_rf,
  220. sum(w11) * 100.0 / nullif(sum(summary), 0) AS p26_rf,
  221. sum(w12) * 100.0 / nullif(sum(summary), 0) AS p28_rf,
  222. sum(w13) * 100.0 / nullif(sum(summary), 0) AS p30_rf
  223. FROM cte_sf_data
  224. ), cte_total_by_fo AS (
  225. SELECT
  226. csd.fo_group,
  227. sum(in_ais) * 100.0 / nullif(sum(csd.summary), 0) AS p4_fo,
  228. sum(w1) * 100.0 / nullif(sum(csd.summary), 0) AS p6_fo,
  229. sum(w2) * 100.0 / nullif(sum(csd.summary), 0) AS p8_fo,
  230. sum(w3) * 100.0 / nullif(sum(csd.summary), 0) AS p10_fo,
  231. sum(w4) * 100.0 / nullif(sum(csd.summary), 0) AS p12_fo,
  232.  
  233. sum(w5) * 100.0 / nullif(sum(csd.summary), 0) AS p14_fo,
  234. sum(w6) * 100.0 / nullif(sum(csd.summary), 0) AS p16_fo,
  235. sum(w7) * 100.0 / nullif(sum(csd.summary), 0) AS p18_fo,
  236. sum(w8) * 100.0 / nullif(sum(csd.summary), 0) AS p20_fo,
  237. sum(w9) * 100.0 / nullif(sum(csd.summary), 0) AS p22_fo,
  238. sum(w10) * 100.0 / nullif(sum(csd.summary), 0) AS p24_fo,
  239. sum(w11) * 100.0 / nullif(sum(csd.summary), 0) AS p26_fo,
  240. sum(w12) * 100.0 / nullif(sum(csd.summary), 0) AS p28_fo,
  241. sum(w13) * 100.0 / nullif(sum(csd.summary), 0) AS p30_fo
  242. FROM cte_sf_data csd
  243. CROSS JOIN cte_total_by_rf rf
  244. GROUP BY fo_group
  245. )
  246.  
  247. SELECT
  248. csd.rn,
  249. csd.total_group,
  250. csd.fo_group,
  251. csd.sf_name,
  252. csd.fo_name,
  253. nullif(csd.summary, 0) AS summary,
  254. nullif(csd.in_ais, 0) AS in_ais,
  255. nullif(csd.p4, 0) AS p4,
  256. nullif(csd.p6, 0) AS p6,
  257. nullif(csd.p8, 0) AS p8,
  258. nullif(csd.p10, 0) AS p10,
  259. nullif(csd.p12, 0) AS p12,
  260. nullif(csd.w1, 0) AS w1,
  261. nullif(csd.w2, 0) AS w2,
  262. nullif(csd.w3, 0) AS w3,
  263. nullif(csd.w4, 0) AS w4,
  264. nullif(csd.w5, 0) AS w5,
  265. nullif(csd.w6, 0) AS w6,
  266. nullif(csd.w7, 0) AS w7,
  267. nullif(csd.w8, 0) AS w8,
  268. nullif(csd.w9, 0) AS w9,
  269. nullif(csd.w10, 0) AS w10,
  270. nullif(csd.w11, 0) AS w11,
  271. nullif(csd.w12, 0) AS w12,
  272. nullif(csd.w13, 0) AS w13,
  273. nullif(csd.treated, 0) AS treated,
  274. nullif(csd.days_for_reply, 0) AS days_for_reply,
  275. nullif(csd.oiv, 0) AS oiv,
  276. nullif(csd.control, 0) AS control,
  277. nullif(csd.control_50, 0) AS control_50,
  278. coalesce(rf.p4_rf, 0) AS p4_rf,
  279. coalesce(rf.p6_rf, 0) AS p6_rf,
  280. coalesce(rf.p8_rf, 0) AS p8_rf,
  281. coalesce(rf.p10_rf, 0) AS p10_rf,
  282. coalesce(rf.p12_rf, 0) AS p12_rf,
  283. coalesce(rf.p14_rf, 0) AS p14_rf,
  284. coalesce(rf.p16_rf, 0) AS p16_rf,
  285. coalesce(rf.p18_rf, 0) AS p18_rf,
  286. coalesce(rf.p20_rf, 0) AS p20_rf,
  287. coalesce(rf.p22_rf, 0) AS p22_rf,
  288. coalesce(rf.p24_rf, 0) AS p24_rf,
  289. coalesce(rf.p26_rf, 0) AS p26_rf,
  290. coalesce(rf.p28_rf, 0) AS p28_rf,
  291. coalesce(rf.p30_rf, 0) AS p30_rf,
  292. coalesce(fo.p4_fo, 0) AS p4_fo,
  293. coalesce(fo.p6_fo, 0) AS p6_fo,
  294. coalesce(fo.p8_fo, 0) AS p8_fo,
  295. coalesce(fo.p10_fo, 0) AS p10_fo,
  296. coalesce(fo.p12_fo, 0) AS p12_fo,
  297. coalesce(fo.p14_fo, 0) AS p14_fo,
  298. coalesce(fo.p16_fo, 0) AS p16_fo,
  299. coalesce(fo.p18_fo, 0) AS p18_fo,
  300. coalesce(fo.p20_fo, 0) AS p20_fo,
  301. coalesce(fo.p22_fo, 0) AS p22_fo,
  302. coalesce(fo.p24_fo, 0) AS p24_fo,
  303. coalesce(fo.p26_fo, 0) AS p26_fo,
  304. coalesce(fo.p28_fo, 0) AS p28_fo,
  305. coalesce(fo.p30_fo, 0) AS p30_fo
  306. FROM cte_sf_data csd
  307. CROSS JOIN cte_total_by_rf rf
  308. JOIN cte_total_by_fo fo ON fo.fo_group = csd.fo_group
  309. ORDER BY csd.rn
  310.  
  311.  
  312.  
  313.  
  314. -- огигинал
  315.  
  316.  
  317. with osn as (with hands AS (
  318. select * from handlings where created_date::text between '2014-11-01' AND '2019-09-06' and status_id !=6)
  319. select gt.name
  320. ,gt.id
  321. ,gt.parent_geo_tag_id
  322. ,gt.level
  323. ,case when gt.level=1 then gt.id when gt.level=2 then gt.parent_geo_tag_id end as rn_parent_geo_tag_id
  324. ,case when (sum (case when h.category_id = 62 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 62 then 1 else 0 end))::INT end as w1 --Сроки
  325. ,case when (sum (case when h.category_id = 67 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 67 then 1 else 0 end))::INT end as w2 --Иное
  326. ,case when (sum (case when h.category_id = 65 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 65 then 1 else 0 end))::INT end as w3 --Соблюдение прав граждан
  327. ,case when (sum (case when h.category_id = 64 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 64 then 1 else 0 end))::INT end as w4 --Условия проживания
  328. ,case when (sum (case when h.category_id = 63 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 63 then 1 else 0 end))::INT end as w5 --Качество
  329. ,case when (sum (case when h.category_id = 1729 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 1729 then 1 else 0 end))::INT end as w6 --Запрос недостоверности
  330. ,case when (sum (case when h.category_id = 850 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 850 then 1 else 0 end))::INT end as w7 --Капремонт
  331. ,case when (sum (case when h.category_id = 66 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 66 then 1 else 0 end))::INT end as w8 --Работа с населением
  332. ,case when (sum (case when h.category_id = 1251 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 1251 then 1 else 0 end))::INT end as w9 --ТКО
  333. ,case when (sum (case when h.category_id = 2660 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 2660 then 1 else 0 end))::INT end as w10 --Сроки проведения капитального ремонта
  334. ,case when (sum (case when h.category_id = 2661 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 2661 then 1 else 0 end))::INT end as w11 --Качество проведенного капитального ремонта
  335. ,case when (sum (case when h.category_id = 2662 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 2662 then 1 else 0 end))::INT end as w12 --Взносы на капитальный ремонт
  336. ,case when (sum (case when h.category_id = 2663 then 1 else 0 END))::INT = '0' then null else (sum (case when h.category_id = 2663 then 1 else 0 end))::INT end as w13 --Разъяснение Законодательства
  337. ,count (*) /*filter (where email like 'rybakova@fondgkh.ru')*/ w0
  338. from geo_tags gt
  339. left join hands h on gt.id = h.region_id
  340. where gt.level in (2,1) --and created_date is not null
  341. group by gt.name, gt.level, gt.id, gt.parent_geo_tag_id
  342. ),
  343.  
  344. treated as (select gt2.geo_tag_id as id
  345. ,count(gt2.geo_tag_name) as treated --Рассмотрено
  346. from(select h.id
  347. ,h.status_id
  348. ,h.surname
  349. ,h.name
  350. ,g.name geo_tag_name
  351. ,g.level geo_tag_level
  352. ,g.parent_geo_tag_id parent_geo_tag_id
  353. ,g.id geo_tag_id
  354. from handlings h
  355. join handlings_status_changes_relations scr on h.id = scr.handling_id
  356. join status_changes sc on scr.statuschange_id = sc.id and sc.is_successfully = 'true'
  357. join (select hr.created ,hr.handling_id
  358. from handling_replies hr
  359. inner join (select id,max(created) over (partition by handling_id) = created is_last_created from handling_replies hr) t on hr.id = t.id where t.is_last_created) hr on h.id = hr.handling_id
  360. left join geo_tags g on h.region_id = g.id
  361. where h.created_date::text between '2014-11-01' AND '2019-09-06' and h.status_id = 9 and hr.created :: TEXT between '2014-11-01' AND '2019-09-06'
  362. group by h.id, g.id
  363. having count(case when sc.to_status_id = 21 then TRUE else null end) = 0
  364. order by h.id) gt2
  365. group by gt2.geo_tag_level, gt2.parent_geo_tag_id, gt2.geo_tag_id
  366. ),
  367.  
  368. days_for_reply as(select gt2.id
  369. ,round(avg(nullif(to_char(hr.created-h.created_date, 'DD'), '')::int),0) as days_for_reply --Время рассмотрения
  370. from handlings h
  371. join (select hr.created, hr.handling_id from handling_replies hr inner join (select id, max(created) over (partition by handling_id) = created is_last_created from handling_replies hr) t on hr.id = t.id where t.is_last_created) hr on h.id = hr.handling_id
  372. join geo_tags gt2 ON gt2.id = h.region_id
  373. where h.status_id in (9,21) and hr.created::text between '2014-11-01' AND '2019-09-06' and category_id != 1729 and NULLIF(to_char(hr.created-h.created_date, 'DD'), '')::int < 90
  374. group by gt2.id,gt2.level,gt2.parent_geo_tag_id
  375. ),
  376.  
  377. oiv as (select gt2.id
  378. ,count(DISTINCT h.id) as oiv --Перенаправлено в ОИВ
  379. from handlings h
  380. join handlings_status_changes_relations hscr on hscr.handling_id = h.id
  381. join (select sc.id, sc.to_status_id, sc.created_at, sc.is_successfully from status_changes sc
  382. inner join (select id, max(created_at) over (partition by hscr.handling_id, sc.to_status_id) = sc.created_at is_last_created
  383. from status_changes sc
  384. inner join handlings_status_changes_relations hscr on sc.id = hscr.statuschange_id) t on t.id = sc.id
  385. where t.is_last_created) sc on hscr.statuschange_id = sc.id and sc.is_successfully = 'true'
  386. join statuses s on s.id = h.status_id
  387. left join geo_tags gt2 on h.region_id = gt2.id
  388. where h.created_date :: TEXT between '2014-11-01' AND '2019-09-06' and sc.created_at :: TEXT between '2014-11-01' AND '2019-09-06' AND sc.to_status_id = 21
  389. group by gt2.id,gt2.name
  390. ),
  391.  
  392. control as (select gt2.id
  393. ,count(h.*) as control --На контролек
  394. from handlings h
  395. left join geo_tags gt2 on h.region_id = gt2.id
  396. where h.status_id = 21 and h.created_date::date between '2014-11-01' AND '2019-09-06'
  397. group by gt2.id
  398. ),
  399.  
  400. control_50 as (select gt2.id
  401. ,count(h.*) as control_50 --На контроле 50
  402. from handlings h
  403. left join geo_tags gt2 on h.region_id = gt2.id
  404. WHERE h.status_id = 21 and h.created_date::date between '2014-11-01' AND '2019-09-06' and (now()::date - created_date::date >50)
  405. group by gt2.id
  406. ),
  407.  
  408. it_sum as(select rank() over (order by w.rn_parent_geo_tag_id,w.id) as rn
  409. ,w.id
  410. ,w.name
  411. ,coalesce(sum(w.w0),0)+coalesce(sum(w.w1),0)+coalesce(sum(w.w2),0)+coalesce(sum(w.w3),0)+coalesce(sum(w.w4),0)+coalesce(sum(w.w5),0)+coalesce(sum(w.w6),0)+coalesce(sum(w.w7),0)++coalesce(sum(w.w8),0)+coalesce(sum(w.w9),0)+coalesce(sum(w.w10),0)+coalesce(sum(w.w11),0)+coalesce(sum(w.w12),0)+coalesce(sum(w.w13),0)as summa
  412. from osn w
  413. group by w.rn_parent_geo_tag_id
  414. ,w.id
  415. ,w.name
  416. order by rn
  417. )
  418.  
  419. select it.rn
  420. ,w.name --1 ( № столбца из шаблона)
  421. ,case when it.summa = 0 then null end --2
  422. ,case when w.w0 = 0 then null else w.w0 end --3
  423. ,w.w1 --5
  424. ,w.w2 --7
  425. ,w.w3 --9
  426. ,w.w4 --11
  427. ,w.w5 --13
  428. ,w.w6 --15
  429. ,w.w7 --17
  430. ,w.w8 --19
  431. ,w.w9 --21
  432. ,w.w10 --23
  433. ,w.w11 --25
  434. ,w.w12 --27
  435. ,w.w13 --29
  436. ,t.treated --31
  437. ,dfr.days_for_reply --32
  438. ,o.oiv --33
  439. ,control --34
  440. ,control_50 --35
  441. from osn w
  442. join it_sum it on it.id=w.id
  443. left join treated t on t.id=w.id
  444. left join days_for_reply dfr on dfr.id=t.id
  445. left join oiv o on o.id=dfr.id
  446. left join control c on c.id=o.id
  447. left join control_50 c50 on c50.id=c.id
  448. order by it.rn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement