Advertisement
aliGo

Untitled

Jun 16th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. select zsl.id,
  2. zsl.period,
  3. zsl.manual_upload,
  4. zsl.family,
  5. zsl.firstname,
  6. zsl.patronymic,
  7. zsl.born_date,
  8. zsl.policy_number,
  9. zsl.usl_ok, -- net
  10. zsl.usl_ok_name,
  11. zsl.lpu_code,
  12. zsl.lpu_name,
  13. zsl.begin_date,
  14. zsl.end_date,
  15. zsl.kd_z,
  16. zsl.rslt,
  17. zsl.rslt_name,
  18. zsl.ishod,
  19. zsl.ishod_name,
  20. zsl.disp_code,
  21. zsl.disp_type,
  22. zsl.vidpom,
  23. zsl.vidpom_name,
  24. zsl.sumv,
  25. zsl.otd_names,
  26. zsl.category_ids,
  27. max(sluch.id) filter (where sluch.is_final_record = true) as final_medical_case_id,
  28. sluch.idzsl,
  29. coalesce(jsonb_agg(distinct v002.name_profil) filter (where v002.name_profil is not null),'[]' :: jsonb) as mp_profiles,
  30. coalesce(jsonb_agg(distinct v020.kp_prname) filter (where v020.kp_prname is not null), '[]' :: jsonb) as bed_profiles,
  31. jsonb_agg(distinct sluch.nhistory) as med_history_numbers,
  32. coalesce(jsonb_agg(distinct v018.name_vid_pom) filter (where v018.name_vid_pom is not null), '[]' :: jsonb) as vid_hmp_names,
  33. coalesce(jsonb_agg(distinct v019.name_vid_hpom) filter (where v019.name_vid_hpom is not null),'[]' :: jsonb) as method_hmp_names,
  34. coalesce(jsonb_agg(distinct jsonb_build_object('pPer', sluch.other ->> 'P_PER', 'pPerName', p_per_dict.pname)) filter (where sluch.other ->> 'P_PER' is not null), '[]' :: jsonb) as p_pers,
  35. max(sluch.diag ->> 'DS1') as ds1,
  36. max(mkb_d.mkb_name) as ds1_name,
  37. coalesce(
  38. jsonb_agg(distinct spec.name) filter (where spec.name <> '' :: text),
  39. jsonb_agg(distinct v021.name_spec) filter (where v021.name_spec <> '' :: text),
  40. '[]' :: jsonb
  41. ) as special_names,
  42. coalesce(jsonb_agg(distinct jsonb_build_object('ksg', sluch.ksg_kpg ->> 'N_KSG', 'ksgName', case when :regionCode = 74 then ksg.name else v023.name_ksg end)) filter (where(sluch.ksg_kpg ->> 'N_KSG') is not null), '[]' :: jsonb) as ksgs,
  43. coalesce(jsonb_agg(distinct sluch_onk.onk ->> 'DS1_T') filter (where sluch_onk.onk ->> 'DS1_T' is not null), '[]' :: jsonb) as ds_onks,
  44. count(distinct sluch.id) as med_case_count,
  45. coalesce(array_agg(distinct v002.code_profil) filter (where v002.code_profil is not null), '{}'::smallint[]) as mp_profile_codes,
  46. coalesce((array_agg(distinct sluch.other -> 'PROFIL_K') filter (where sluch.other ->> 'PROFIL_K' is not null))::smallint[], '{}'::smallint[]) as bed_profile_codes,
  47. coalesce(jsonb_agg(distinct sluch.hmp ->> 'VID_HMP') filter (where sluch.hmp ->> 'VID_HMP' is not null), '[]'::jsonb) as vid_hmp_codes,
  48. coalesce((array_agg(distinct sluch.hmp ->> 'METOD_HMP') filter (where sluch.hmp ->> 'METOD_HMP' is not null))::smallint[], '{}'::smallint[]) as method_hmp_codes,
  49. coalesce(jsonb_agg(distinct sluch.diag ->> 'DS2') filter (where sluch.diag ->> 'DS2' is not null), '[]'::jsonb) as ds2,
  50. coalesce(jsonb_agg(distinct sluch.diag ->> 'DS3') filter (where sluch.diag ->> 'DS3' is not null), '[]'::jsonb) as ds3,
  51. coalesce(array_agg(distinct sluch.ksg_kpg ->> 'N_KSG') filter (where (sluch.ksg_kpg ->> 'N_KSG') is not null), '{}'::text[]) as ksg_codes,
  52. coalesce(array_agg(distinct sluch.ksg_kpg ->> 'N_KPG') filter (where (sluch.ksg_kpg ->> 'N_KPG') is not null), '{}'::text[]) as kpg_codes,
  53. coalesce((array_agg(distinct sluch.med ->> 'PRVS') filter (where sluch.med ->> 'PRVS' is not null))::integer[], '{}'::integer[]) as special_codes,
  54. podr_name
  55. from ( (
  56. select zsl.id as id,
  57. schet_period.period_date as period,
  58. zsl.manual_upload,
  59. person.fam as family,
  60. person.im as firstname,
  61. person.ot as patronymic,
  62. person.dr as born_date,
  63. person.npolis as policy_number,
  64. v006.code_type_usl as usl_ok,
  65. v006.name_type_usl as usl_ok_name,
  66. f003.mcode as lpu_code,
  67. f003.nam_mok as lpu_name,
  68. zsl.date_begin as begin_date,
  69. zsl.date_end as end_date,
  70. (zsl.other ->> 'KD_Z')::smallint as kd_z,
  71. CASE WHEN (schet.iddisp is null) THEN v009.code_resl ELSE v017.code_disp_resl END as rslt,
  72. CASE WHEN (schet.iddisp is null) THEN v009.name_resl::text ELSE v017.name_disp_resl::text END as rslt_name,
  73. v012.code_ishod::smallint as ishod,
  74. v012.name_ishod as ishod_name,
  75. v016.code_disp as disp_code,
  76. v016.name_disp as disp_type,
  77. (zsl.med_pom ->> 'VIDPOM')::smallint as vidpom,
  78. v008.vmpname as vidpom_name,
  79. (zsl.sum ->> 'SUMV')::numeric as sumv,
  80. '[]'::jsonb as otd_names,
  81. (select coalesce(jsonb_agg(distinct selected_zsl.category_id), '[]'::jsonb) from bills.selected_zsl where selected_zsl.zsl_id = zsl.id ) as category_ids,
  82. coalesce(podr_omsk.nam_mop, podr_tula.name) as podr_name
  83. from bills.schet
  84. join bills.schet_period on schet_period.id = schet.idperiod
  85. join bills.zsl on zsl.idschet = schet.id
  86. join bills.sluch sl on sl.idzsl = zsl.id and sl.is_final_record = true
  87. join pacients.person on person.id = zsl.idperson
  88. join directory.f003 on f003.id = zsl.idmedorg
  89. left join directory.podr_omsk podr_omsk on schet.idfilial = directory.getfilialidbycode(55)
  90. and podr_omsk.lpu_1::varchar = sl.lpu ->> 'LPU_1'
  91. and zsl.date_end between podr_omsk.date_begin and coalesce(podr_omsk.date_end, '9999-01-01')
  92. left join directory.t_podr_tula podr_tula on schet.idfilial = directory.getfilialidbycode(71)
  93. and podr_tula.podr_id = sl.lpu ->> 'LPU_1'
  94. left join directory.v006 v006 on v006.id = zsl.idtypeusl
  95. left join directory.v008 v008 on v008.idvmp::text = zsl.med_pom ->> 'VIDPOM'
  96. left join directory.v009 on v009.id = zsl.idresult
  97. left join directory.v017 on v017.id = zsl.idresult
  98. left join directory.v012 on v012.id = zsl.idishod
  99. left join directory.v016 on v016.id = schet.iddisp
  100. left join bills.not_visible_zsl nvz on zsl.id=nvz.zsl_id
  101. where schet.idfilial = directory.getfilialidbycode(:regionCode)
  102. and nvz.id is null
  103. and schet_period.period_date between :periodBeginDate and :periodEndDate
  104. and exists(
  105. select 1 from bills.sluch
  106. left join directory.v002 on v002.id = sluch.idprofil
  107. where sluch.idzsl = zsl.id %sluchFilters% ) %filters%
  108. order by %order% lower(person.fam), lower(person.im), lower(person.ot), zsl.date_begin, zsl.date_end, zsl.id
  109. limit :limit offset :offset)) zsl
  110. join bills.sluch on sluch.idzsl = zsl.id
  111. left join bills.usl on usl.idsluch = sluch.id
  112. left join directory.v002 on v002.id = sluch.idprofil
  113. left join directory.v020 v020 on v020.idk_pr::text = sluch.other ->> 'PROFIL_K'
  114. and sluch.date_end between v020.date_begin and v020.date_end
  115. left join directory.p_per p_per_dict on p_per_dict.p_per_id::text = sluch.other ->> 'P_PER'
  116. left join directory.v018 v018 on v018.code_vid_pom = sluch.hmp ->> 'VID_HMP'
  117. and sluch.date_end between v018.begin_date and v018.end_date
  118. left join directory.v019 v019 on v019.code_vid_hpom::text = sluch.hmp ->> 'METOD_HMP'
  119. and sluch.date_end between v019.begin_date and v019.end_date
  120. left join directory.mkb mkb_d on mkb_d.mkb_code = sluch.diag ->> 'DS1'
  121. left join directory.special spec on (sluch.other -> 'COD_SPEC')::int = spec.npp and :regionCode = 74
  122. left join directory.v021 v021 on v021.code_spec :: text = sluch.med ->> 'PRVS' and :regionCode != 74
  123. and sluch.date_end >= v021.begin_date
  124. and sluch.date_begin <= v021.end_date
  125. left join directory.v023 v023 on v023.idump = zsl.usl_ok
  126. and v023.code_ksg = (sluch.ksg_kpg ->> 'N_KSG') and sluch.date_end >= v023.begin_date and (v023.end_date is null or sluch.date_end <= v023.end_date)
  127. left join directory.ksg on ksg.usl_ok = zsl.usl_ok
  128. and ksg.ksg_num2 = (sluch.ksg_kpg ->> 'N_KSG') and sluch.date_end >= date(to_timestamp(ksg.date_beg2 / 1000) at time zone 'Europe/Moscow')
  129. and (ksg.date_end2 is null or sluch.date_end <= date(to_timestamp(ksg.date_end2 / 1000) at time zone 'Europe/Moscow'))
  130. left join bills.sluch_onk on sluch_onk.idsluch = sluch.id
  131. group by sluch.idzsl,
  132. zsl.id,
  133. zsl.period,
  134. zsl.family,
  135. zsl.firstname,
  136. zsl.patronymic,
  137. zsl.born_date,
  138. zsl.policy_number,
  139. zsl.usl_ok,
  140. zsl.usl_ok_name,
  141. zsl.lpu_code,
  142. zsl.lpu_name,
  143. zsl.begin_date,
  144. zsl.end_date,
  145. zsl.kd_z,
  146. zsl.rslt,
  147. zsl.rslt_name,
  148. zsl.ishod,
  149. zsl.ishod_name,
  150. zsl.disp_code,
  151. zsl.disp_type,
  152. zsl.vidpom,
  153. zsl.vidpom_name,
  154. zsl.sumv,
  155. zsl.otd_names,
  156. zsl.category_ids,
  157. zsl.manual_upload,
  158. zsl.podr_name
  159. order by %order% lower(zsl.family), lower(zsl.firstname), lower(zsl.patronymic), zsl.begin_date, zsl.end_date, zsl.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement