
            SELECT
                'ttms'|| cast(TTMS.TimetableMedService_id as varchar) as "item_key",
                EUP.EvnUslugaPar_id as "EvnUslugaPar_id",
                ED.EvnDirection_id as "EvnDirection_id",
                ED.Diag_id as "Diag_id",
                ED.EvnStatus_id as "EvnStatus_id",
                ED.EvnDirection_Num as "EvnDirection_Num",
                null as "EvnQueue_id",
                TTMS.TimetableMedService_id as "TimetableMedService_id",
                case when TTMS.TimetableMedService_begTime is not null
                    then to_char(cast(TTMS.TimetableMedService_begTime as timestamp(3)),'dd.mm.yyyy')
                    else to_char(cast(TTMS.TimetableMedService_factTime as timestamp(3)),'dd.mm.yyyy')
                end as "TimetableMedService_begDate",
                EUP.Person_id as "Person_id",
                case when exists(
                    select *
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = EUP.Person_id
                    and PQ.PersonQuarantine_endDT is null
                ) then 'true' else 'false' end as "PersonQuarantine_IsOn",
                EUP.PersonEvn_id as "PersonEvn_id",
                EUP.Server_id as "Server_id",
                EUP.UslugaComplex_id as "UslugaComplex_id",
                EPCU.EvnPrescrConsUsluga_id as "EvnPrescr_id",
                coalesce(EPCU.EvnPrescrConsUsluga_IsExec, 1) as "EvnPrescr_IsExec",
                EPCU.PrescriptionStatusType_id as "PrescriptionStatusType_id",
                13 as "PrescriptionType_id",
                EPP.Evn_id as "EvnPrescrParentEvn_id",
                EPP.Evn_rid as "EvnPrescrParentEvn_rid",
                EPP.EvnClass_SysNick as "EvnPrescrParentEvnClass_SysNick",
                LpuFrom.Lpu_Nick as "Lpu_Name",
                LpuSectionFrom.LpuSection_Name as "LpuSection_Name",
                case when 2 = coalesce(EUP.EvnUslugaPar_IsCito, ED.EvnDirection_IsCito, 1) then 'true' else 'false' end as "EvnUslugaPar_isCito",
                case when 2 = COALESCE(EPCU.EvnPrescrConsUsluga_IsExec, 1) OR 15 = ED.EvnStatus_id then 'true' else 'false' end as "Usluga_IsHasReception",
                to_char (ED.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                coalesce(to_char (TTMS.TimetableMedService_begTime, 'HH24:MI'),'б/з') as "TimetableMedService_begTime",
                PS.Person_Fio as "Person_FIO",
                UC.UslugaComplex_Name as "UslugaComplex_Name",
                MP.Person_Fin as "MedPersonal_FIO",
                D.Diag_FullName as "Diag_Name",
                case when UCMS.UslugaComplexMedService_id IS NULL then 'Общее' else UC.UslugaComplex_Name end as "TimetableMedServiceType",
                UCMS.UslugaComplex_id as "UslugaComplexRecord_id",
                MPF.EvnDirection_From as "EvnDirection_From",
                null as "UslugaComplexMedService_id" -- услуга для записи
            FROM v_TimetableMedService_lite TTMS
                left join v_UslugaComplexMedService UCMS on UCMS.UslugaComplexMedService_id = TTMS.UslugaComplexMedService_id
                left join v_UslugaComplex UC on UC.UslugaComplex_id = UCMS.UslugaComplex_id
                left join v_EvnDirection_all ED on TTMS.EvnDirection_id = ED.EvnDirection_id
                    and ED.EvnDirection_failDT is null
                    and ED.EvnStatus_id not in (12,13)
                left join v_EvnUslugaPar EUP on EUP.EvnDirection_id = ED.EvnDirection_id
                left join lateral (select * from v_Person_all PS where ED.PersonEvn_id = PS.PersonEvn_id AND ED.Server_id = PS.Server_id limit 1) PS on true
                left join v_Lpu LpuFrom on LpuFrom.Lpu_id = ED.Lpu_id
                left join v_LpuSection LpuSectionFrom on LpuSectionFrom.LpuSection_id = ED.LpuSection_id
                left join v_Diag D on D.Diag_id = ED.Diag_id
                left join v_EvnPrescrDirection EPD on EPD.EvnDirection_id = ED.EvnDirection_id
                left join v_EvnPrescrConsUsluga EPCU on EPCU.EvnPrescrConsUsluga_id = EPD.EvnPrescr_id
                left join v_EvnUslugaCommon EUC on EUC.EvnPrescr_id = EUP.EvnPrescr_id
                left join lateral (select * from v_Evn EPP where EPP.Evn_id = EPCU.EvnPrescrConsUsluga_pid limit 1) EPP on true
                left join lateral (select * from v_EvnPL EPL where EPL.EvnDirection_id = ED.EvnDirection_id limit 1) EPL on true
                left join v_EvnVizitPL EVPL on EVPL.EvnDirection_id = ED.EvnDirection_id
                left join lateral (select * from v_MedPersonal MP where coalesce(EPL.MedPersonal_id,EVPL.MedPersonal_id,EUC.MedPersonal_id) = MP.MedPersonal_id limit 1) MP on true
                LEFT JOIN LATERAL (
                    select
                        (l.Lpu_Nick || COALESCE(' / ' || LS.LpuSection_Name, '') || COALESCE(' / ' || MP.Person_Fio, '')) as EvnDirection_From
                    from
                        v_MedPersonal MP
                        left join v_LpuSection LS on ED.LpuSection_id = LS.LpuSection_id
                        left join v_Lpu l on l.Lpu_id = ED.Lpu_sid
                    where
                        MP.MedPersonal_id = ED.MedPersonal_id
                    limit 1
                ) MPF ON TRUE
            WHERE
                (TTMS.MedService_id = '890101000001083' OR UCMS.MedService_id = '890101000001083')
            AND cast(TTMS.TimetableMedService_begTime as date)
                between cast('2022-05-03' as date) and cast('2022-05-03' as date)
        
            LIMIT 10000
        
