    select
        ed.EvnDirection_id as "EvnDirection_id",
        ed.EvnDirection_pid as "EvnDirection_pid",
        ed.Person_id as "Person_id",
        ed.PayType_id as "PayType_id",
        ed.DirType_id as "DirType_id",
        ed.EvnDirection_setDT::timestamp(0) as "EvnDirection_setDT",
        ed.EvnDirection_setDT::date as "EvnDirection_setDate",
        ed.Diag_id as "Diag_id",
        d.Diag_Code as "Diag_Code",
        d.Diag_SCode as "Diag_SCode",
        ed.EvnDirection_Num as "EvnDirection_Num",
        ed.EvnDirection_Descr as "EvnDirection_Descr",
        ed.Lpu_sid as "Lpu_sid",
        ed.MedPersonal_id as "MedPersonal_id",
        ed.MedStaffFact_id as "MedStaffFact_id",
        ed.Lpu_did as "Lpu_did",
        ed.LpuSection_id as "LpuSection_id",
        ed.LpuSectionProfile_id as "LpuSectionProfile_id",
        ed.EvnDirection_failDT::timestamp(0) as "EvnDirection_failDT",
        ed.TimeTableMedService_id as "TimeTableMedService_id",
        ep.EvnPrescr_id as "EvnPrescr_id",
        ep.PrescriptionType_id as "PrescriptionType_id",
        ep.EvnPrescr_IsCito as "EvnPrescr_IsCito",
        ed.EvnQueue_id as "EvnQueue_id",
        ed.EvnDirection_insDT::timestamp(0) as "EvnDirection_insDT",
        ed.EvnDirection_updDT::timestamp(0) as "EvnDirection_updDT",
        ps.Person_SurName as "PersonSurName_MedPersonal",
        ps.Person_FirName as "PersonFirName_MedPersonal",
        ps.Person_SecName as "PersonSecName_MedPersonal",
        o1.Org_Name as "Lpu_Name_sid",
        o2.Org_Name as "Lpu_Name_did",
        ed.EvnDirection_isAuto as "EvnDirection_isAuto",
        ed.EvnClass_id as "EvnClass_id"
        , ed.Lpu_id as "Lpu_id"
    from
        v_EvnDirection_all ed
        inner join Lpu on lpu.Lpu_id = ed.Lpu_id and lpu.Region_id in (52)
        left join Diag d on d.Diag_id = ed.Diag_id
        left join v_MedPersonalCache mp on mp.MedPersonal_id = ed.MedPersonal_id and mp.Lpu_id = ed.Lpu_id
        left join v_PersonState ps on ps.person_id = mp.person_id
        left join Lpu l1 on l1.Lpu_id = ed.Lpu_sid and (l1.Region_id in (52) or l1.Region_id is null)
        left JOIN v_Org o1 on o1.Org_id = l1.Org_id
        left join Lpu l2 on l2.Lpu_id = ed.Lpu_did and (l2.Region_id in (52) or l2.Region_id is null)
        left JOIN v_Org o2 on o2.Org_id = l2.Org_id
        left join lateral (
            select
                ep.EvnPrescr_id,
                ep.PrescriptionType_id,
                ep.EvnPrescr_IsCito
            from
                EvnPrescrDirection epd
                inner join v_EvnPrescr ep on ep.EvnPrescr_id = epd.EvnPrescr_id
            where
                epd.EvnDirection_id = ed.EvnDirection_id
            order by
                ep.EvnPrescr_id desc
                offset 0 rows fetch next 1 rows only
        ) ep on true
    where (1=1)
         and (ed.Lpu_did = '520101000000011' and
         exists(select 1
                from
                    v_EvnLabRequest lr
                    inner join v_EvnLabSample ls on ls.EvnLabRequest_id = lr.EvnLabRequest_id
                where
                    lr.EvnDirection_id = ed.EvnDirection_id
                     and (ls.EvnLabSample_setDT::timestamp(0) >= '2023-07-16 00:00:00')))
    order by
        ed.EvnDirection_id
        offset 0 rows fetch next 100 rows only
