Advertisement
aliGo

Untitled

Jun 6th, 2022
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 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. (select coalesce(jsonb_agg(distinct selected_zsl.category_id), '[]'::jsonb) from bills.selected_zsl where selected_zsl.zsl_id = zsl.id ) as 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 directory.v006 v006 on v006.id = zsl.idtypeusl
  87. left join directory.v008 v008 on v008.idvmp::text = zsl.med_pom ->> 'VIDPOM'
  88. left join directory.v009 on v009.id = zsl.idresult
  89. left join directory.v017 on v017.id = zsl.idresult
  90. left join directory.v012 on v012.id = zsl.idishod
  91. left join directory.v016 on v016.id = schet.iddisp
  92. left join bills.not_visible_zsl nvz on zsl.id=nvz.zsl_id
  93. where schet.idfilial = directory.getfilialidbycode(:regionCode)
  94. and nvz.id is null and schet_period.period_date between :periodBeginDate and :periodEndDate and exists(
  95. select 1 from bills.sluch
  96. left join directory.v002 on v002.id = sluch.idprofil
  97. where sluch.idzsl = zsl.id ) and (bills.zsl.sum->>'OPLATA')::smallint is distinct from 2 AND EXISTS (SELECT 1 FROM bills.selected_zsl WHERE zsl_id = zsl.id AND category_id = :categoryId)
  98. order by lower(person.fam), lower(person.im), lower(person.ot), zsl.date_begin, zsl.date_end, zsl.id
  99. limit :limit offset :offset)) zsl
  100. join bills.sluch on sluch.idzsl = zsl.id
  101. left join bills.usl on usl.idsluch = sluch.id
  102. left join directory.v002 on v002.id = sluch.idprofil
  103. left join directory.v020 v020 on v020.idk_pr::text = sluch.other ->> 'PROFIL_K'
  104. and sluch.date_end between v020.date_begin and v020.date_end
  105. left join directory.p_per p_per_dict on p_per_dict.p_per_id::text = sluch.other ->> 'P_PER'
  106. left join directory.v018 v018 on v018.code_vid_pom = sluch.hmp ->> 'VID_HMP'
  107. and sluch.date_end between v018.begin_date and v018.end_date
  108. left join directory.v019 v019 on v019.code_vid_hpom::text = sluch.hmp ->> 'METOD_HMP'
  109. and sluch.date_end between v019.begin_date and v019.end_date
  110. left join directory.mkb mkb_d on mkb_d.mkb_code = sluch.diag ->> 'DS1'
  111. left join directory.special spec on (sluch.other -> 'COD_SPEC')::int = spec.npp and :regionCode = 74
  112. left join directory.v021 v021 on v021.code_spec :: text = sluch.med ->> 'PRVS' and :regionCode != 74
  113. and sluch.date_end >= v021.begin_date
  114. and sluch.date_begin <= v021.end_date
  115. left join directory.v023 v023 on v023.idump = zsl.usl_ok
  116. 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)
  117. left join directory.ksg on ksg.usl_ok = zsl.usl_ok
  118. 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')
  119. and (ksg.date_end2 is null or sluch.date_end <= date(to_timestamp(ksg.date_end2 / 1000) at time zone 'Europe/Moscow'))
  120. left join bills.sluch_onk on sluch_onk.idsluch = sluch.id
  121. group by sluch.idzsl,
  122. zsl.id,
  123. zsl.period,
  124. zsl.family,
  125. zsl.firstname,
  126. zsl.patronymic,
  127. zsl.born_date,
  128. zsl.policy_number,
  129. zsl.usl_ok,
  130. zsl.usl_ok_name,
  131. zsl.lpu_code,
  132. zsl.lpu_name,
  133. zsl.begin_date,
  134. zsl.end_date,
  135. zsl.kd_z,
  136. zsl.rslt,
  137. zsl.rslt_name,
  138. zsl.ishod,
  139. zsl.ishod_name,
  140. zsl.disp_code,
  141. zsl.disp_type,
  142. zsl.vidpom,
  143. zsl.vidpom_name,
  144. zsl.sumv,
  145. zsl.otd_names,
  146. zsl.category_ids,
  147. zsl.manual_upload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement