Advertisement
aliGo

Untitled

Jun 16th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 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. from ( (
  55. select zsl.id as id,
  56. schet_period.period_date as period,
  57. zsl.manual_upload,
  58. person.fam as family,
  59. person.im as firstname,
  60. person.ot as patronymic,
  61. person.dr as born_date,
  62. person.npolis as policy_number,
  63. v006.code_type_usl as usl_ok,
  64. v006.name_type_usl as usl_ok_name,
  65. f003.mcode as lpu_code,
  66. f003.nam_mok as lpu_name,
  67. zsl.date_begin as begin_date,
  68. zsl.date_end as end_date,
  69. (zsl.other ->> 'KD_Z')::smallint as kd_z,
  70. CASE WHEN (schet.iddisp is null) THEN v009.code_resl ELSE v017.code_disp_resl END as rslt,
  71. CASE WHEN (schet.iddisp is null) THEN v009.name_resl::text ELSE v017.name_disp_resl::text END as rslt_name,
  72. v012.code_ishod::smallint as ishod,
  73. v012.name_ishod as ishod_name,
  74. v016.code_disp as disp_code,
  75. v016.name_disp as disp_type,
  76. (zsl.med_pom ->> 'VIDPOM')::smallint as vidpom,
  77. v008.vmpname as vidpom_name,
  78. (zsl.sum ->> 'SUMV')::numeric as sumv,
  79. '[]'::jsonb as otd_names,
  80. s.category_ids
  81. from bills.schet
  82. join bills.schet_period on schet_period.id = schet.idperiod
  83. join bills.zsl on zsl.idschet = schet.id
  84. join pacients.person on person.id = zsl.idperson
  85. join directory.f003 on f003.id = zsl.idmedorg
  86. left join (select s.zsl_id
  87. , coalesce(jsonb_agg(distinct s.category_id), '[]'::jsonb) as category_ids
  88. from bills.getzslidbycategoryid(:regionCode,:category_id, :periodBeginDate, :periodEndDate) s
  89. group by s.zsl_id) s on s.zsl_id = zsl.id
  90. left join directory.v006 v006 on v006.id = zsl.idtypeusl
  91. left join directory.v008 v008 on v008.idvmp::text = zsl.med_pom ->> 'VIDPOM'
  92. left join directory.v009 on v009.id = zsl.idresult
  93. left join directory.v017 on v017.id = zsl.idresult
  94. left join directory.v012 on v012.id = zsl.idishod
  95. left join directory.v016 on v016.id = schet.iddisp
  96. left join bills.not_visible_zsl nvz on zsl.id=nvz.zsl_id
  97. where schet.idfilial = directory.getfilialidbycode(:regionCode)
  98. and nvz.id is null
  99. and zsl.id = case when :category_id is not null then s.zsl_id else zsl.id end
  100. and schet_period.period_date between :periodBeginDate and :periodEndDate
  101. and exists(
  102. select 1 from bills.sluch
  103. left join directory.v002 on v002.id = sluch.idprofil
  104. where sluch.idzsl = zsl.id )
  105. and (bills.zsl.sum->>'OPLATA')::smallint is distinct from 2
  106. order by lower(person.fam), lower(person.im), lower(person.ot), zsl.date_begin, zsl.date_end, zsl.id
  107. limit :limit offset :offset)) zsl
  108. join bills.sluch on sluch.idzsl = zsl.id
  109. left join bills.usl on usl.idsluch = sluch.id
  110. left join directory.v002 on v002.id = sluch.idprofil
  111. left join directory.v020 v020 on v020.idk_pr::text = sluch.other ->> 'PROFIL_K'
  112. and sluch.date_end between v020.date_begin and v020.date_end
  113. left join directory.p_per p_per_dict on p_per_dict.p_per_id::text = sluch.other ->> 'P_PER'
  114. left join directory.v018 v018 on v018.code_vid_pom = sluch.hmp ->> 'VID_HMP'
  115. and sluch.date_end between v018.begin_date and v018.end_date
  116. left join directory.v019 v019 on v019.code_vid_hpom::text = sluch.hmp ->> 'METOD_HMP'
  117. and sluch.date_end between v019.begin_date and v019.end_date
  118. left join directory.mkb mkb_d on mkb_d.mkb_code = sluch.diag ->> 'DS1'
  119. left join directory.special spec on (sluch.other -> 'COD_SPEC')::int = spec.npp and :regionCode = 74
  120. left join directory.v021 v021 on v021.code_spec :: text = sluch.med ->> 'PRVS' and :regionCode != 74
  121. and sluch.date_end >= v021.begin_date
  122. and sluch.date_begin <= v021.end_date
  123. left join directory.v023 v023 on v023.idump = zsl.usl_ok
  124. 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)
  125. left join directory.ksg on ksg.usl_ok = zsl.usl_ok
  126. 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')
  127. and (ksg.date_end2 is null or sluch.date_end <= date(to_timestamp(ksg.date_end2 / 1000) at time zone 'Europe/Moscow'))
  128. left join bills.sluch_onk on sluch_onk.idsluch = sluch.id
  129. group by sluch.idzsl,
  130. zsl.id,
  131. zsl.period,
  132. zsl.family,
  133. zsl.firstname,
  134. zsl.patronymic,
  135. zsl.born_date,
  136. zsl.policy_number,
  137. zsl.usl_ok,
  138. zsl.usl_ok_name,
  139. zsl.lpu_code,
  140. zsl.lpu_name,
  141. zsl.begin_date,
  142. zsl.end_date,
  143. zsl.kd_z,
  144. zsl.rslt,
  145. zsl.rslt_name,
  146. zsl.ishod,
  147. zsl.ishod_name,
  148. zsl.disp_code,
  149. zsl.disp_type,
  150. zsl.vidpom,
  151. zsl.vidpom_name,
  152. zsl.sumv,
  153. zsl.otd_names,
  154. zsl.category_ids,
  155. zsl.manual_upload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement