Advertisement
temaon_lieto

Untitled

Jun 6th, 2024
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EXPLAIN ANALYZE
  2. WITH events AS (
  3.     SELECT
  4.         calendar_events.id,
  5.         calendar_events.period_start,
  6.         calendar_events.legal_entity_id,
  7.         encounter_reasons.title_translations -> 'uk' as reason_name,
  8.         calendar_events.patient_id,
  9.         calendar_events.comment,
  10.         calendar_events.employee_id
  11.     FROM
  12.         calendar_events
  13.     LEFT JOIN encounter_reasons ON calendar_events.encounter_reason_id IN (249, 250) AND calendar_events.encounter_reason_id = encounter_reasons.id
  14.     WHERE calendar_events.legal_entity_id = 9045 AND calendar_events.period_start BETWEEN '2024-03-01 09:00:00.000000' AND '2024-03-30 09:00:00.000000'
  15. ), employees AS (
  16.     SELECT
  17.         dbo."Employees"."Id" as id,
  18.         dbo."PositionRefs"."_Name" as position,
  19.         dbo."Parties"."LegalEntityId" as legal_entity_id,
  20.         dbo."Parties"."FirstName" as first_name,
  21.         dbo."Parties"."LastName" as last_name,
  22.         dbo."Parties"."SecondName" as second_name,
  23.         CONCAT_WS(' ', dbo."Parties"."LastName", dbo."Parties"."FirstName", dbo."Parties"."SecondName") as full_name
  24.     FROM dbo."Employees"
  25.     JOIN dbo."Parties" ON dbo."Employees"."PartyId" = dbo."Parties"."Id"
  26.     JOIN dbo."PositionRefs" ON dbo."Employees"."PositionRefId" = dbo."PositionRefs"."Id"
  27.     WHERE dbo."Parties"."LegalEntityId" = 9045
  28. ), patients AS (
  29.     SELECT
  30.         user_patients.id,
  31.         user_patients.legal_entity_id,
  32.         personalities.first_name,
  33.         personalities.last_name,
  34.         personalities.second_name,
  35.         CONCAT_WS(' ', personalities.last_name, personalities.first_name, personalities.second_name) as full_name
  36.     FROM user_patients
  37.     JOIN personalities ON user_patients.personality_id = personalities.id
  38.     WHERE user_patients.legal_entity_id = 9045
  39. ), services AS (
  40.     SELECT
  41.         medical_service_orders.id,
  42.         medical_service_orders.patient_id,
  43.         medical_service_orders.employee_id,
  44.         medical_service_orders.legal_entity_id,
  45.         medical_service_orders.orderable_id,
  46.         medical_service_orders.orderable_type,
  47.         medical_service_services.title_translations -> 'uk' as service_title
  48.     FROM medical_service_orders
  49.     JOIN medical_service_service_orders ON medical_service_service_orders.medical_service_order_id = medical_service_orders.id
  50.     JOIN medical_service_services ON medical_service_services.id = medical_service_service_orders.medical_service_service_id
  51.     WHERE medical_service_orders.legal_entity_id = 9045
  52. )
  53. SELECT
  54.     DISTINCT ON(events.id)
  55.     patients.full_name as "ПІБ пацієнта",
  56.     employees.full_name as "ПІБ лікаря",
  57.     employees.position as "посада лікаря",
  58.     events.period_start as "дата прийому",
  59.     events.reason_name as "тип прийому",
  60.     array_agg(services.service_title) as "послуги",
  61.     events.comment as "коментар"
  62. FROM events
  63.     LEFT JOIN employees ON events.employee_id = employees.id
  64.     LEFT JOIN patients ON events.patient_id = patients.id
  65.     LEFT JOIN services ON services.orderable_id = events.id AND services.orderable_type = 'Ehr::Visit'
  66. GROUP BY events.id, patients.full_name, employees.full_name, employees.position, events.period_start, events.reason_name, events.comment
  67. LIMIT 1
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement