Advertisement
GLASHATAY_007

Untitled

Jun 30th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.17 KB | None | 0 0
  1. CREATE TEMP TABLE expertize
  2. (
  3. lid int4 PRIMARY KEY,
  4. procedure_id int4,
  5. boss varchar(200),
  6. boss_date DATE,
  7. komitet varchar(200),
  8. komitet_date DATE,
  9. tsmets varchar(200),
  10. tsmets_date DATE,
  11. control_date_End DATE,
  12. expertize_date_End DATE,
  13. expertize_date_plan DATE
  14. )
  15. ;
  16.  
  17. -- заполняем времнную таблицу eshop_rej
  18. INSERT INTO expertize
  19. SELECT
  20. l.id lid,
  21. e.procedure_id,
  22. MAX(CASE WHEN e.boss_id IS NOT NULL THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) boss,
  23. MAX(CASE WHEN e.boss_id IS NOT NULL THEN e.expertize_date_plan ELSE NULL END) boss_date,
  24. MAX(CASE WHEN e.department_id = 10564 THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) komitet,
  25. MAX(CASE WHEN e.department_id = 10564 THEN e.expertize_date_plan ELSE NULL END) komitet_date,
  26. MAX(CASE WHEN e.department_id IN (10565,57725) THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) tsmets,
  27. MAX(CASE WHEN e.department_id IN (10565,57725) THEN e.expertize_date_plan ELSE NULL END) tsmets_date,
  28. MAX(control_date_End) AS control_date_End,
  29. MAX(expertize_date_End) AS expertize_date_End,
  30. MAX(expertize_date_plan) AS expertize_date_plan
  31. FROM po_procedure_expertize e
  32. LEFT JOIN lots l ON e.procedure_id = l.procedure_id
  33. JOIN users u ON COALESCE(e.boss_id,e.user_id) = u.id
  34. GROUP BY e.procedure_id, l.id
  35. HAVING DATE_PART('year', MAX(CASE WHEN e.boss_id IS NOT NULL THEN e.expertize_date_plan ELSE NULL END)) IN (2022, 2023)
  36. ORDER BY procedure_id;
  37.  
  38.  
  39. -- создаем временную таблицу cons_lot выводим те лоты по которым больше 1 заказчика т.е. совместные
  40. CREATE TEMP TABLE hist AS (
  41. SELECT record_id,
  42. COUNT(DATE) AS count_date_uo
  43. FROM (SELECT record_id, DATE
  44. FROM po_procedure_hIstory
  45. WHERE field = 'sed_registration_date' AND record_id IN (SELECT procedure_id FROM expertize)) a
  46. GROUP BY record_id);
  47.  
  48. CREATE TEMP TABLE nmck AS (
  49. SELECT l.procedure_id,
  50. SUM(start_price) AS nmсk
  51. FROM procedures pr
  52. JOIN lots l ON l.procedure_id = pr.id
  53. GROUP BY l.procedure_id);
  54.  
  55.  
  56.  
  57. CREATE TEMP TABLE date_c AS (
  58. SELECT pr.id, record_id,
  59. DATE date_all,
  60. ROW_NUMBER() OVER (PARTITION BY record_id ORDER BY DATE) AS rating,
  61. pr.date_notice_published,
  62. CASE WHEN DATE::DATE<=COALESCE(pr.date_notice_published::DATE, NOW()) THEN 1 ELSE 0 END date_publ_sr,
  63. psto.step_id,
  64. CASE WHEN DATE_PART('year', DATE::DATE) < 2023 AND psto.step_id = 'assign_expert_uo' THEN 1
  65. WHEN DATE_PART('year', DATE::DATE) >= 2023 AND psto.step_id = 'wait_expert_uo' THEN 1
  66. WHEN psto.step_id = 'recall_request_review' THEN 1
  67. WHEN psto.step_id = 'expertize_complete' THEN 1
  68. ELSE 0 END step_priznak
  69. FROM (
  70. SELECT DATE,
  71. CASE WHEN "from"='NULL' THEN NULL ELSE "from"::INTEGER END "from",
  72. CASE WHEN "to"='NULL' THEN NULL ELSE "to"::INTEGER END "to",
  73. lh.record_id
  74. FROM lots_history lh
  75. WHERE lh.record_id IN (SELECT DISTINCT l.id
  76. FROM expertize e
  77. LEFT JOIN lots l ON l.procedure_id=e.procedure_id)
  78. AND lh.field = 'current_step'
  79. ORDER BY DATE
  80. ) a
  81. LEFT JOIN procedure_steps psfrom ON psfrom.id = a.FROM
  82. LEFT JOIN procedure_steps psto ON psto.id = a.TO
  83. JOIN lots l ON l.id = a.record_id
  84. JOIN procedures pr ON l.procedure_id = pr.id
  85. WHERE (psto.step_id = 'expertize_complete' OR
  86. psto.step_id = 'assign_expert_uo' OR
  87. psto.step_id = 'recall_request_review' OR
  88. psto.step_id = 'wait_expert_uo')
  89. AND
  90. (CASE WHEN DATE_PART('year', DATE::DATE) < 2023 AND psto.step_id = 'assign_expert_uo' THEN 1
  91. WHEN DATE_PART('year', DATE::DATE) >= 2023 AND psto.step_id = 'wait_expert_uo' THEN 1
  92. WHEN psto.step_id = 'recall_request_review' THEN 1
  93. WHEN psto.step_id = 'expertize_complete' THEN 1
  94. ELSE 0 END) = 1
  95. ORDER BY record_id, DATE);
  96.  
  97.  
  98.  
  99. SELECT
  100. a.* ,
  101. RANK() OVER(ORDER BY a.step_id,a.rating) rnk,
  102. ROW_NUMBER() OVER ( PARTITION BY a.step_id ) rn
  103. FROM date_c a
  104. LEFT JOIN (SELECT record_id, date_all, rating-1 rating, step_id FROM date_c) b ON a.record_id = b.record_id AND a.rating = b.rating
  105. JOIN lots l ON l.id = a.record_id
  106. JOIN procedures pr ON l.procedure_id = pr.id
  107. WHERE a.id=4311985
  108. ORDER BY a.rating;
  109.  
  110.  
  111. create temp table date_count AS (
  112. SELECT record_id lot_id,
  113. min(case when a.step_id in ('assign_expert_uo','wait_expert_uo' ) then a.date_alla else null end) min_date_uo,
  114. string_agg(exp_w_d::varchar ,';' ORDER BY date_alla) c_exp_wd, -- Количество рабочих дней после начала экспертизы УО
  115. string_agg(red_w_d::varchar ,';' ORDER BY date_alla) c_red_wd, --Количество рабочих дней после опубликования заключения УО
  116. sum(exp_w_d) s_exp_wd, -- Общее количество рабочих дней нахождения заявки на экспертизе
  117. sum(red_w_d) s_red_wd, -- Общее количество рабочих дней нахождения заявки на доработке
  118.  
  119. string_agg(date_all_do::varchar,'; ' ORDER BY date_alla) AS date_all_do,
  120. string_agg(date_all_posle::varchar,'; ' ORDER BY date_alla) AS date_all_posle,
  121. sum(count_zakl_do) count_zakl_do,
  122. sum(count_postup_no)count_postup_no,
  123. sum(count_zakl_posle) count_zakl_posle,
  124. string_agg(a.step_id_do,'; ' ORDER BY date_alla) AS steps_do,
  125. string_agg(a.step_id_posle,'; 'ORDER BY date_alla) AS steps_posle,
  126. max(case when rait_tek_st =1 then a.step_id_do else null end) status_now_pr
  127. FROM (
  128. SELECT a.record_id, a.step_id,
  129. CASE WHEN a.rating%2=1 and a.date_publ_sr = 1 and b.step_id<>'recall_request_review' THEN (SELECT SUM(type_::int) FROM sppr.work_days_all WHERE date_
  130. BETWEEN a.date_all::date + INTERVAL '1 day' AND b.date_all::date) ELSE null END exp_w_d,
  131. CASE WHEN a.rating%2=0 and a.date_publ_sr = 1 and a.step_id<>'recall_request_review' THEN (SELECT SUM(type_::int) FROM sppr.work_days_all WHERE date_
  132. BETWEEN a.date_all::date + INTERVAL '1 day' AND b.date_all::date) ELSE null END red_w_d,
  133. CASE WHEN a.rating%2=1 THEN b.date_all - a.date_all ELSE null END exp,
  134. CASE WHEN a.rating%2=0 then b.date_all - a.date_all ELSE null END red,
  135. CASE WHEN a.date_publ_sr = 1 and a.step_id<>'recall_request_review' then a.date_all::varchar
  136. WHEN a.date_publ_sr = 1 and a.step_id='recall_request_review' then a.date_all::varchar ||'*'
  137. ELSE null
  138. END date_all_do,
  139. CASE WHEN a.date_publ_sr = 0 and a.step_id<>'recall_request_review' then a.date_all::varchar
  140. WHEN a.date_publ_sr = 0 and a.step_id='recall_request_review' then a.date_all::varchar ||'*'
  141. ELSE null
  142. END date_all_posle,
  143. b.date_all,
  144. a.date_all date_alla,
  145. case when a.step_id='wait_expert_uo' then 1 else 0 end count_postup_no,
  146. case when a.date_publ_sr = 1 and b.step_id='expertize_complete' then 1 else 0 end count_zakl_do,
  147. case when a.date_publ_sr = 0 and b.step_id='expertize_complete' then 1 else 0 end count_zakl_posle,
  148. case when a.date_publ_sr = 1 and a.step_id = 'assign_expert_uo' then 'Назначение экспертизы в УО'
  149. when a.date_publ_sr = 1 and a.step_id = 'wait_expert_uo' then 'На экспертизе заявки на закупку в УО'
  150. when a.date_publ_sr = 1 and a.step_id = 'expertize_complete' then 'Экспертиза завершена'
  151. when a.date_publ_sr = 1 and a.step_id = 'recall_request_review' then 'Рассмотрение запроса на отзыв'
  152. end step_id_do,
  153. case when a.date_publ_sr = 0 and a.step_id = 'assign_expert_uo' then 'Назначение экспертизы в УО'
  154. when a.date_publ_sr = 0 and a.step_id = 'wait_expert_uo' then 'На экспертизе заявки на закупку в УО'
  155. when a.date_publ_sr = 0 and a.step_id = 'expertize_complete' then 'Экспертиза завершена'
  156. when a.date_publ_sr = 0 and a.step_id = 'recall_request_review' then 'Рассмотрение запроса на отзыв'
  157. end step_id_posle,
  158. case when a.date_publ_sr = 1 then row_number() over (partition by a.record_id order by a.date_all desc) else null end rait_tek_st
  159. FROM date_c a
  160. LEFT JOIN (select record_id, date_all, rating-1 rating, step_id from date_c) b ON a.record_id = b.record_id AND a.rating = b.rating
  161. JOIN lots l on l.id = a.record_id
  162. JOIN procedures pr on l.procedure_id = pr.id
  163. --WHERE b.record_id IS NOT NULL
  164. )a
  165. GROUP BY record_id)
  166. ;
  167. create temp table fin AS (
  168. SELECT
  169. l.procedure_id,
  170. SUM(CASE WHEN pvtp.name = 2023 THEN pf.amount ELSE NULL END ) amount_23,
  171. SUM(CASE WHEN pvtp.name = 2024 THEN pf.amount ELSE NULL END ) amount_24
  172. FROM expertize exp
  173. JOIN lots l ON l.procedure_id = exp.procedure_id
  174. JOIN po_finances pf On pf.lot_id = l.id
  175. JOIN po_account pa On pa.id = pf.po_account_id
  176. JOIN po_budget pb On pb.id = pa.po_budget_id
  177. JOIN po_vocab_time_periods pvtp On pvtp.id = pb.po_period_id
  178. GROUP BY l.procedure_id)
  179. ;
  180. create temp table exp_date AS (
  181. SELECT MAX(date) expertize_complete_date,
  182. record_id
  183. FROM expertize exp
  184. JOIN lots l ON l.procedure_id = exp.procedure_id
  185. JOIN lots_history ps ON ps.record_id = l.id
  186. WHERE ps."to" in (
  187. SELECT id::varchar
  188. FROM procedure_steps ps
  189. WHERE ps.step_id = 'edit_request')
  190. GROUP BY record_id
  191. )
  192. ;
  193. create temp table cst AS (
  194. SELECT id,
  195. string_agg( article_code,'; ') AS cel_st
  196. FROM (SELECT DISTINCT l.id, dea.article_code
  197. FROM expertize exp
  198. JOIN lots l ON l.procedure_id = exp.procedure_id
  199. JOIN po_finances pf on pf.lot_id = l.id
  200. JOIN po_account pa on pa.id = pf.po_account_id
  201. JOIN po_exp_account pea on pea.id = pa.po_exp_account_id
  202. LEFT JOIN gpo_po_exp_account gpea on gpea.po_exp_code_id = pea.id
  203. LEFT JOIN gpo_exp_account dea on dea.id = gpea.gpo_exp_account_id
  204. JOIN po_budget pb On pb.id = pa.po_budget_id
  205. JOIN po_vocab_time_periods pvtp On pvtp.id = pb.po_period_id
  206. ) q
  207. GROUP BY id
  208. )
  209. ;
  210. create temp table fin_con AS (
  211. SELECT
  212. l.procedure_id,
  213. SUM(CASE WHEN pvtp.name = 2023 AND pf.type =1 THEN pf.amount ELSE NULL END) amountcf_23,
  214. SUM(CASE WHEN pvtp.name = 2023 AND pf.type =2 THEN pf.amount ELSE NULL END) amountc_23,
  215. SUM(CASE WHEN pvtp.name = 2024 AND pf.type =1 THEN pf.amount ELSE NULL END) amountc_24
  216. FROM expertize exp
  217. JOIN lots l ON l.procedure_id = exp.procedure_id
  218. JOIN cm_contract_finances pf ON (pf.lot_id = l.id AND pf.type In (1,2) AND pf.actual IS TRUE)
  219. JOIN po_account pa ON pa.id = pf.po_account_id
  220. JOIN po_budget pb ON pb.id = pa.po_budget_id
  221. JOIN po_vocab_time_periods pvtp On pvtp.id = pb.po_period_id
  222. GROUP BY l.procedure_id)
  223. ;
  224. create temp table ppr AS (
  225. SELECT ppr.procedure_id,
  226. COUNT(*) count_procedure_recall
  227. FROM expertize exp
  228. JOIN po_procedure_recall_request ppr ON ppr.procedure_id = exp.procedure_id
  229. WHERE actual IS TRUE
  230. GROUP BY ppr.procedure_id
  231. )
  232.  
  233.  
  234. SELECT
  235.  
  236. c_grbs.full_name AS grbsname, -- ГРБС
  237. c.full_name, --заказчик
  238. c1.full_name AS customer, --организатор
  239. vpt.name AS way, --СОП
  240. pr.title AS purchase,-- наименование закупки
  241. n.nmсk, -- нмцк
  242. date_count.count_postup_no, -- поступило на экспертизу (общее количество считается от количества направлено на экспертизу)
  243. date_c.nach_exp_act, -- дата начала экспертизы
  244. date_count.count_zakl_do,-- количество заключений
  245. ppr.count_procedure_recall,-- количество отзывов
  246. date_count.c_exp_wd, -- Количество рабочих дней после начала экспертизы УО
  247. date_count.s_exp_wd, -- Общее количество рабочих дней нахождения заявки на экспертизе
  248. vps.full_name lotstage, -- текущий статус
  249. cc.contract_amount, -- цена контракта
  250. n.nmсk - coalesce(cc.contract_amount,0) eco -- цена контракта снижения
  251. ,'' -- примечение (пустое поле)
  252.  
  253. /*,date_part('year',min_date_uo), pr.id AS id_procedure, min_date_uo,
  254. pp.request_order_number,
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. case when pl.supply_impossible is true then 'да' else null end supply_impossible,
  262. --stages_exp.date_all,
  263. --stages_exp.date_all_recall,
  264. ppr.count_procedure_recall,
  265. h.count_date_uo,
  266. --date_count.c_exp,
  267. --date_count.c_red,
  268. --date_count.s_exp,
  269. --date_count.s_red,
  270. date_count.c_exp_wd,
  271. date_count.c_red_wd,
  272. case when date_count.s_exp_wd is null then 0 else date_count.s_exp_wd end s_exp_wd,
  273. case when date_count.s_red_wd is null then 0 else date_count.s_red_wd end s_red_wd,
  274. date_count.date_all_do,
  275. date_count.steps_do,
  276. date_count.date_all_posle,
  277. date_count.steps_posle,
  278. date_count.count_zakl_do,
  279. date_count.count_zakl_posle,
  280. l.date_fulfilled,
  281. l.date_placed,
  282. f.amount_23,
  283. f.amount_24,
  284. exp.expertize_date_plan::date, -- Дата завершения контроля (план)
  285. CASE WHEN vps.full_name ='Редактирование заявки на закупку' THEN (SELECT sum(type_::int) FROM sppr.work_days_all WHERE date_
  286. BETWEEN exp_date.expertize_complete_date::date + INTERVAL '1 day' AND current_date) ELSE NULL END work_days,
  287. exp.komitet,
  288. exp.tsmets,
  289. exp.control_date_End::date,
  290. pr.regIstry_number,
  291. pl.purchase_code,
  292. cst.cel_st,
  293. cc.contract_rnk,
  294. cc.contract_amount,
  295. cc.date_sign::date,
  296. fc.amountcf_23,
  297. fc.amountc_23,
  298. fc.amountc_24,
  299. pr.date_notice_published::date,
  300. date_count.status_now_pr
  301. */
  302. FROM procedures pr
  303. JOIN po_procedures pp ON pr.id = pp.procedure_id
  304. JOIN expertize exp ON exp.procedure_id = pr.id
  305. JOIN vocab_procedure_types vpt ON pr.procedure_type = vpt.id
  306. JOIN lots l ON l.procedure_id = pr.id
  307. JOIN lot_customers lc ON lc.lot_id = l.id AND lc.actual = TRUE
  308. JOIN contragents c ON c.id = lc.customer_id
  309. JOIN contragents c1 ON c1.id = pr.organizer_contragent_id
  310. JOIN po_list_org_hierarchy ploh ON ploh.contragent_id = c.id AND ploh.actual = TRUE AND ploh.parent_type = 1
  311. JOIN contragents c_grbs ON c_grbs.id = ploh.parent_contragent_id
  312. JOIN procedure_steps ps ON ps.id = l.current_step
  313. JOIN vocab_procedure_steps vps ON vps.pseudo::text = ps.step_id::text And vps.actual = true
  314. LEFT JOIN nmck n ON n.procedure_id = pr.id
  315. LEFT JOIN hist h ON h.record_id = pr.id
  316. --LEFT JOIN stages_exp ON stages_exp.lot_id = l.id
  317. LEFT JOIN date_count ON date_count.lot_id=l.id
  318. LEFT JOIN fin f ON f.procedure_id = pr.id
  319. LEFT JOIN exp_date ON exp_date.record_id=l.id
  320. LEFT JOIN po_lots pl On pl.lot_id = l.id
  321. LEFT JOIN cst ON cst.id = l.id
  322. LEFT JOIN cm_contracts cc ON l.id = cc.lot_id
  323. LEFT JOIN fin_con fc ON fc.procedure_id = pr.id
  324. LEFT JOIN ppr ON ppr.procedure_id = pr.id
  325. left Join (SELECT
  326. max(case when step_id='wait_expert_uo' then date_all else null end) nach_exp_act,
  327. id FROM date_c group by id )date_c on date_c.id =pr.id
  328. WHERE date_part('year',min_date_uo)=2023
  329. AND pr.id <> 4173048 -- по просьбе Имедашвили Лаша убрать закупку, ошибочно вышедшую на экспертизу 28/02/23
  330. -- and pr.id='4311985'
  331. ;
  332.  
  333.  
  334.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement