Advertisement
GLASHATAY_007

Untitled

Jun 30th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Create Temp Table expertise
  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 expertise
  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 expertise)) 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 expertise 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. select * from date_c where id = 4178069 limit 123
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement