Advertisement
aliGo

Загадка_дыры

Apr 27th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. with m_zpmd as (
  2. select zpmd.id,
  3. max(mo.mcode::text) as mcode,
  4. case
  5. when zpmd.region = 'TULA'::zpmd.region then max(mo_tula.short_name::text)
  6. else max(mo.nam_mok::text) end as mo_short_name,
  7. case
  8. when zpmd.region = 'TULA'::zpmd.region then max(mo_tula.full_name::text)
  9. else max(mo.nam_mop::text) end as mo_full_name,
  10. max(mo.id) as mo_id,
  11. zpmd.request_name,
  12. zpmd.request_number,
  13. max(v006.name_type_usl::text) as usl_name,
  14. zpmd.usl_ok as usl_id,
  15. zpmd.date_create,
  16. zpmd.date_send,
  17. zpmd.status::text as status,
  18. COALESCE(concat_ws(' ', max(u.last_name), max(u.first_name)),'') as user_full_name,
  19. COALESCE(max(u.id::text), ''::text) as user_id,
  20. max(zpmd.region)::text as region,
  21. max(zpmd.theme_category_id::text) as theme_category_id,
  22. max(expertise_class_decomposed.expertise_type)::text as expertise_type,
  23. max(category.id::text) as category_id,
  24. max(theme.id::text) as theme_id,
  25. max(category.name::text) as category_name,
  26. max(theme.name::text) as theme_name,
  27. zpmd.related_document_id,
  28. zpmd.has_related_document,
  29. rpzpmd_parent.parent_zpmd_id,
  30. rpzpmd_repeated.repeated_zpmd_id,
  31. zpmd.podr_id,
  32. zpmd.is_migrated
  33. from zpmd.zpmd zpmd
  34. join directory.f003 mo on zpmd.medical_organization_code = mo.mcode
  35. join directory.v006 v006 on v006.code_type_usl = zpmd.usl_ok
  36. and current_date >= v006.begin_date and current_date <= v006.end_date
  37. left join directory.mo_tula mo_tula on zpmd.medical_organization_code = mo_tula.mo_code
  38. and zpmd.region = 'TULA'::zpmd.region
  39. left join zpmd.users u on u.id = zpmd.user_id
  40. join expert.theme_category on zpmd.theme_category_id = theme_category.id
  41. join expert.category on theme_category.category_id = category.id
  42. join expert.expertise_class_decomposed
  43. on expertise_class_decomposed.expertise_class = theme_category.expertise_class
  44. left join expert.theme on theme_category.theme_id = theme.id
  45. left join zpmd.repeated_parent_zpmd_map rpzpmd_repeated on rpzpmd_repeated.parent_zpmd_id = zpmd.id
  46. left join zpmd.repeated_parent_zpmd_map rpzpmd_parent on rpzpmd_parent.repeated_zpmd_id = zpmd.id
  47. group by zpmd.id, rpzpmd_parent.parent_zpmd_id, rpzpmd_repeated.repeated_zpmd_id
  48. order by max(zpmd.date_create) desc),
  49. item_info as (
  50. select zpmdi.zpmd_id,
  51. count( zpmdi.id) filter
  52. (where zpmdi.status not in ('CREATED'::zpmd.zpmd_item_status, 'DELETED'::zpmd.zpmd_item_status)
  53. and zpmdi.availability ='AVAILABLE'::zpmd.zpmd_item_availability) as received_count,
  54. count( zpmdi.id) filter (where zpmdi.availability = 'AVAILABLE'::zpmd.zpmd_item_availability
  55. and zpmdi.status <> 'DELETED'::zpmd.zpmd_item_status) as total_count,
  56. count( zpmdi.id) filter ( where ia.zpmd_item_id is not null ) as with_created_act_count,
  57. max( zpmdi.id_profil) as max_profile_id,
  58. min( zpmdi.id_profil) as min_profile_id,
  59. max( zpmdi.date_receive) as max_date_receive
  60. from zpmd.zpmd_item zpmdi
  61. left join zpmd.zpmd_item_acts ia on zpmdi.id = ia.zpmd_item_id and not ia.is_deleted
  62. where (( ?::text is null
  63. and ?::text is null
  64. and ?::text is null
  65. and ?::text is null
  66. and ?::date is null
  67. and ?::date is null)
  68. or exists (select id
  69. from zpmd.get_zpmd_items_by_pacient(?::text, ?::text, ?::text, ?::text, ?::date, ?::date)
  70. where id = zpmdi.id))
  71. group by zpmdi.zpmd_id),
  72. zpmd_view as (
  73. select m.id,
  74. COALESCE(podr_tula.name, podr_omsk.nam_mop) as podr_name,
  75. COALESCE(podr_tula.id, podr_omsk.id) as podr_id,
  76. COALESCE(podr_tula.podr_id, podr_omsk.lpu_1::varchar) as podr_code,
  77. m.mcode,
  78. m.mo_short_name,
  79. m.mo_full_name,
  80. m.mo_id,
  81. m.request_name,
  82. m.request_number,
  83. m.usl_name,
  84. m.usl_id,
  85. m.date_create,
  86. m.date_send,
  87. case when (m.status = 'RECEIVED') then
  88. item_info.max_date_receive - m.date_send
  89. else current_date - m.date_send
  90. end as diff,
  91. m.status,
  92. m.user_full_name,
  93. m.user_id,
  94. m.region,
  95. m.theme_category_id,
  96. m.expertise_type,
  97. m.theme_id,
  98. m.category_id,
  99. m.theme_name,
  100. m.category_name,
  101. COALESCE(item_info.total_count, 0) as total,
  102. COALESCE(item_info.received_count, 0) as received,
  103. case item_info.min_profile_id = item_info.max_profile_id
  104. when true then v002.name_profil
  105. when false then 'нескольким профилям'
  106. else '' end as profile,
  107. m.parent_zpmd_id,
  108. m.repeated_zpmd_id,
  109. m.related_document_id,
  110. m.has_related_document,
  111. m.is_migrated,
  112. item_info.with_created_act_count > 0 as has_act,
  113. item_info.with_created_act_count > 0 and
  114. item_info.with_created_act_count = item_info.total_count as all_items_has_act
  115. from m_zpmd m
  116. left join item_info on item_info.zpmd_id = m.id
  117. left join directory.v002 on v002.id = item_info.max_profile_id
  118. left join directory.podr_omsk podr_omsk on m.region = 'OMSK' and podr_omsk.id = m.podr_id
  119. left join directory.t_podr_tula podr_tula on m.region = 'TULA' and podr_tula.id = m.podr_id)
  120. select * from zpmd_view zv where 1=1 and zv.region = ? and zv.status in (?, ?, ?, ?)
  121. and zv.is_migrated in (?) and zv.all_items_has_act in (?)
  122. order by date_create DESC limit ? offset ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement