
            --variables
            with cte as (select localtimestamp as curDate),
            ED1 as (
            select
                
            ED.*,
            EUT.EvnUslugaTelemed_id EUT_EvnUslugaTelemed_id,
            EUT.UslugaTelemedResultType_id EUT_UslugaTelemedResultType_id,
            EUT.LpuSectionProfile_id EUT_LpuSectionProfile_id,
            r.EvnClass_SysNick r_EvnClass_SysNick,
            coalesce(LSPT.LpuSectionProfile_name, LSP.LpuSectionProfile_Name) LpuSectionProfile_Name,
            esh.EvnStatusCause_id EvnStatusCause_id,
            d.Diag_FullName Diag_FullName,
            coalesce(isDel.isDel, 0) isDel
            
        
            from v_EvnDirection_all ED
            
            INNER JOIN v_Evn r on r.Evn_id = ED.EvnDirection_rid
            LEFT JOIN LATERAL (
                select
                    EUT.EvnUslugaTelemed_id,
                    EUT.EvnUslugaTelemed_setDT,
                    EUT.UslugaTelemedResultType_id,
                    EUT.LpuSectionProfile_id
                    
                from v_EvnUslugaTelemed EUT
                where EUT.EvnDirection_id = ED.EvnDirection_id
                limit 1
            ) EUT on true
            LEFT JOIN v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
            left join lateral (
                select LSPT.LpuSectionProfile_id, LSPT.LpuSectionProfile_name
                from v_LpuSectionProfile LSPT
                where LSPT.LpuSectionProfile_id = EUT.LpuSectionProfile_id limit 1
            ) LSPT on true
            LEFT JOIN LATERAL (
                select
                    esh.EvnStatusCause_id,
                    esh.pmUser_updID
                from
                    v_EvnStatusHistory esh
                where
                    esh.Evn_id = ED.EvnDirection_id
                    and esh.EvnStatus_id = ED.EvnStatus_id
                    and esh.EvnStatusCause_id is not null
                order by
                    esh.EvnStatusHistory_id desc
                limit 1
            ) esh on true
            LEFT JOIN lateral
            (
                select d.Diag_fullName from v_Diag d where ED.Diag_id = d.Diag_id limit 1
            ) d on true
            LEFT JOIN LATERAL(
                select
                1 as isDel
                from v_pmUserCache pm
                where pm.pmuser_id = ed.pmuser_insid
                limit 1
            ) isDel on true
            LEFT JOIN v_PersonState PS on PS.Person_id = ED.Person_id
        
            where (1=1)
                
           and (
                (EUT.EvnUslugaTelemed_setDT is null AND ED.EvnDirection_failDT is null)
                OR (EUT.EvnUslugaTelemed_setDT is not null AND cast(EUT.EvnUslugaTelemed_setDT as date) between '2023-07-24' and '2023-07-24')
                OR (ED.EvnDirection_failDT is not null AND cast(ED.EvnDirection_failDT as date) between '2023-07-24' and '2023-07-24')
                OR ED.EvnDirection_setDT between '2023-07-24' and '2023-07-24'
           )
        
                and (ED.EvnDirection_IsConcilium = 1 or ED.EvnDirection_IsConcilium is null)
                
                 AND ED.RequestConsult_id = '2'
            ORDER by EvnDirection_setDT DESC
            OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
        ),
            ED2 as (
            select
                
            ED.*,
            EUT.EvnUslugaTelemed_id EUT_EvnUslugaTelemed_id,
            EUT.UslugaTelemedResultType_id EUT_UslugaTelemedResultType_id,
            EUT.LpuSectionProfile_id EUT_LpuSectionProfile_id,
            r.EvnClass_SysNick r_EvnClass_SysNick,
            coalesce(LSPT.LpuSectionProfile_name, LSP.LpuSectionProfile_Name) LpuSectionProfile_Name,
            esh.EvnStatusCause_id EvnStatusCause_id,
            d.Diag_FullName Diag_FullName,
            coalesce(isDel.isDel, 0) isDel
            
        
            from v_EvnDirection_all ED
            
            INNER JOIN v_Evn r on r.Evn_id = ED.EvnDirection_rid
            LEFT JOIN LATERAL (
                select
                    EUT.EvnUslugaTelemed_id,
                    EUT.EvnUslugaTelemed_setDT,
                    EUT.UslugaTelemedResultType_id,
                    EUT.LpuSectionProfile_id
                    
                from v_EvnUslugaTelemed EUT
                where EUT.EvnDirection_id = ED.EvnDirection_id
                limit 1
            ) EUT on true
            LEFT JOIN v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
            left join lateral (
                select LSPT.LpuSectionProfile_id, LSPT.LpuSectionProfile_name
                from v_LpuSectionProfile LSPT
                where LSPT.LpuSectionProfile_id = EUT.LpuSectionProfile_id limit 1
            ) LSPT on true
            LEFT JOIN LATERAL (
                select
                    esh.EvnStatusCause_id,
                    esh.pmUser_updID
                from
                    v_EvnStatusHistory esh
                where
                    esh.Evn_id = ED.EvnDirection_id
                    and esh.EvnStatus_id = ED.EvnStatus_id
                    and esh.EvnStatusCause_id is not null
                order by
                    esh.EvnStatusHistory_id desc
                limit 1
            ) esh on true
            LEFT JOIN lateral
            (
                select d.Diag_fullName from v_Diag d where ED.Diag_id = d.Diag_id limit 1
            ) d on true
            LEFT JOIN LATERAL(
                select
                1 as isDel
                from v_pmUserCache pm
                where pm.pmuser_id = ed.pmuser_insid
                limit 1
            ) isDel on true
            LEFT JOIN v_PersonState PS on PS.Person_id = ED.Person_id
        
            WHERE (1=1)
                
           and (
                (EUT.EvnUslugaTelemed_setDT is null AND ED.EvnDirection_failDT is null)
                OR (EUT.EvnUslugaTelemed_setDT is not null AND cast(EUT.EvnUslugaTelemed_setDT as date) between '2023-07-24' and '2023-07-24')
                OR (ED.EvnDirection_failDT is not null AND cast(ED.EvnDirection_failDT as date) between '2023-07-24' and '2023-07-24')
                OR ED.EvnDirection_setDT between '2023-07-24' and '2023-07-24'
           )
        
                and ED.EvnDirection_IsConcilium = 2
                 AND ED.RequestConsult_id = '2'
            ORDER by EvnDirection_setDT DESC
            OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
         )
            --end variables
            SELECT
                -- select
                *
                -- end select
            FROM
                -- from
                (
                
            SELECT
                ED1.EvnClass_id as "EvnClass_id",
                ED1.Diag_FullName as "Diag_FullName",
                ED1.EvnDirection_id as "EvnDirection_id",
                ED1.EvnDirection_pid as "EvnDirection_pid",
                ED1.EvnDirection_rid as "EvnDirection_rid",
                ED1.r_EvnClass_SysNick as "RootEvnClass_SysNick",
                ED1.EUT_EvnUslugaTelemed_id as "EvnUslugaTelemed_id",
                CF.ConsultingForm_Name as "ConsultingForm_Name",
                ED1.Diag_id as "Diag_id",
                ED1.Person_id as "Person_id",
                ED1.PersonEvn_id as "PersonEvn_id",
                ED1.Server_id as "Server_id",
                ED1.EvnDirection_Num as "EvnDirection_Num",
                ED1.PayType_id as "PayType_id",
                to_char(ED1.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                to_char(ED1.EvnDirection_insDT, 'dd.mm.yyyy') as "EvnDirection_insDate",
                to_char(ED1.EvnDirection_insDT, 'hh24:MI') as "EvnDirection_insTime",
                v_Lpu.Lpu_Nick as "Lpu_Nick",
                v_Lpu.Lpu_id as "Lpu_id",
                pp.RiskType_id as "RiskType_id",
                mp.Person_Fin as "Person_Fin",
                mp2.Person_Fin as "MedPersonalFIO_whom_sent",
                (PS.Person_SurName || ' ' || Coalesce(PS.Person_FirName, '') || ' ' || Coalesce(PS.Person_SecName,'')) as "Person_FIO",
                case when 2 = Coalesce(ED1.EvnDirection_IsCito,1) then 'true' else 'false' end as "EvnDirection_IsCito",
                RCC.RemoteConsultCause_Name as "RemoteConsultCause_Name",
                ED1.LpuSectionProfile_Name,
                UTRT.UslugaTelemedResultType_Name as "UslugaTelemedResultType_Name",
                consForm.ConsultationForm_Code as "ConsultationForm_Code",
                consForm.ConsultationForm_Name as "ConsultationForm_Name",
                ED1.isDel as "isDel",
                case
                    when consForm.ConsultationForm_Code is null
                        then ''
                    when consForm.ConsultationForm_Code = '1'
                        then to_char(DATEADD('day',14,ED1.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED1.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '2'
                        then to_char(DATEADD('day',1,ED1.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED1.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '3'
                        then to_char(DATEADD('hour',2,ED1.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(dateadd('hour',2,ED1.EvnDirection_insDT), 'hh24:MI')
                end as "deadLine",
                case
                    when consForm.ConsultationForm_Code is null
                        then 99999999
                    when consForm.ConsultationForm_Code = '1'
                        then abs(DATE_PART('day', DATEADD('day',14,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',14,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '2'
                        then abs(DATE_PART('day', DATEADD('day',1,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',1,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '3'
                        then abs(DATE_PART('day', DATEADD('day',2,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',2,ED1.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                end as "deadDiff",
                DT.DirType_Code as "DirType_Code",
                ED1.EvnQueue_id as "EvnQueue_id",
                ED1.EvnQueue_id as "EvnQueue_id",
                ED1.TimeTableStac_id as "TimeTableStac_id",
                ED1.TimeTableGraf_id as "TimeTableGraf_id",
                ED1.TimeTableMedService_id as "TimeTableMedService_id",
                ESC.EvnStatusCause_Name as "EvnStatusCause_Name",
                case
                    when ED1.EvnDirection_failDT is not null
                        then DFT.DirFailType_Name
                    when EC.EvnConcilium_id is not null and ES2.EvnStatus_SysNick in ('Confirmed', 'Posted')
                        then NCR.ConciliumResult_Name
                    else
                        UTRT.UslugaTelemedResultType_Name
                end as "evndirection_result",
                ex.EvnXml_id as "EvnXml_id",
                ex.EvnXml_IsSigned as "EvnXml_IsSigned",
                to_char(ex.EvnXml_signDT, 'dd.mm.yyyy') as "EvnXml_signDT",
                coalesce(pu2.pmUser_Name, pu.pmUser_Name) as "pmUser_signName",
                ED1.RequestConsult_id as "RequestConsult_id",
                EC.EvnConcilium_id as "EvnConcilium_id",
                EC.ConciliumType_id as "ConciliumType_id",
                ED1.MedService_id as "MedService_id",
                LpuD.Org_id as "Org_tid",
                ED1.RemoteConsultCause_id as "RemoteConsultCause_id",
                ED1.RecMethodType_id as "RecMethodType_id",
                ED1.EvnDirection_isAuto as "EvnDirection_isAuto",
                MS.MedService_Nick as "MedService_Nick",
                CASE WHEN COALESCE(PD.PersonDisp_endDate, (select curDate from cte)) >= (select curDate from cte)
                    THEN 1
                    ELSE 2
                END as "isDisp",
                coalesce(EC.EvnConcilium_IsSigned, ex.EvnXml_IsSigned) as "Evn_IsSigned",
                to_char(coalesce(EC.EvnConcilium_signDT, ex.EvnXml_signDT), 'dd.mm.yyyy') as "Evn_signDT",
                ES2.EvnStatus_SysNick as "EvnConciliumStatus_SysNick",
                ES.EvnStatus_SysNick as "EvnDirectionStatus_SysNick",
                1 as "isConciliumMember",
                CR.ConciliumResults_id as "ConciliumResults_id",
                ED1.pmUser_insID as "pmUser_insID",
                ED1.UslugaComplex_did as "UslugaComplex_did",
                
            case
                -- Если услуга отмененная
                when ED1.EvnDirection_failDT is not null
                    then 5
                -- Если услуга оказана или статус направления Обслужено (15)
                when ED1.EUT_EvnUslugaTelemed_id is not null or ED1.EvnStatus_id = 15
                    then 4
                when ED1.EUT_EvnUslugaTelemed_id is null and (ED1.EvnStatus_id is null or ED1.EvnStatus_id <> 15)
                    then case
                            -- Если направление на службу
                            when ED1.MedPersonal_did is null
                                then 2
                            -- Если направление на врача
                            when ED1.MedPersonal_did is not null
                                then 3
                        end
            end as "evndirection_group"
        
                ,case
                when ED1.TimeTableGraf_id is not null then
                    to_char(ttgl.TimeTableGraf_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttgl.TimeTableGraf_begTime, 'hh24:MI')
                else
                    to_char(ttmsl.TimeTableMedService_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttmsl.TimeTableMedService_begTime, 'hh24:MI')
                end as "TimeTable_begTime"
                , ED1.ConsultingForm_id as "ConsultingForm_id"
                
            FROM
                ED1
                left join v_PersonPregnancy pp  on pp.Person_id = ED1.Person_id
                LEFT JOIN ConsultingForm CF  on CF.ConsultingForm_id = ED1.ConsultingForm_id
                LEFT JOIN v_PersonState PS  on PS.Person_id = ED1.Person_id
                left join v_EvnXml ex on ex.Evn_id = ED1.EUT_EvnUslugaTelemed_id
                left join v_pmUser pu on pu.pmUser_id = ex.pmUser_signID
                left join v_UslugaTelemedResultType UTRT  on UTRT.UslugaTelemedResultType_id = ED1.EUT_UslugaTelemedResultType_id
                LEFT JOIN v_RemoteConsultCause RCC  on RCC.RemoteConsultCause_id = ED1.RemoteConsultCause_id
                LEFT JOIN v_Lpu  on v_Lpu.Lpu_id = ED1.Lpu_sid
                LEFT JOIN v_Lpu LpuD on LpuD.Lpu_id = ED1.Lpu_did
                LEFT JOIN v_MedPersonal mp  on mp.MedPersonal_id = ED1.MedPersonal_id and mp.Lpu_id = ED1.Lpu_sid
                LEFT JOIN v_MedPersonal mp2 on mp2.MedPersonal_id = ED1.MedPersonal_did and mp2.Lpu_id = ED1.Lpu_did
                LEFT JOIN v_ConsultationForm consForm on consForm.ConsultationForm_id = ED1.ConsultationForm_id
                LEFT JOIN v_DirType DT on DT.DirType_id = ED1.DirType_id
                LEFT JOIN v_EvnStatus ES  on ES.EvnStatus_id = ED1.EvnStatus_id
                LEFT JOIN v_EvnConcilium EC on EC.EvnConcilium_pid = ED1.EvnDirection_id
                LEFT JOIN v_EvnStatus ES2 on ES2.EvnStatus_id = EC.EvnStatus_id
                LEFT JOIN v_ConciliumResult CR on CR.EvnConcilium_id = EC.EvnConcilium_id
                left join nsi.v_ConciliumResult NCR on NCR.ConciliumResult_id = CR.ConciliumResults_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED1.EvnDirection_id
                left join v_EvnStatusCause esc on esc.EvnStatusCause_id = ED1.EvnStatusCause_id
                left join v_MedService MS on MS.MedService_id = ED1.MedService_id
                left join v_MedServiceType MST on MST.MedServiceType_id = MS.MedServiceType_id
                left join v_PersonDisp PD on PD.Person_id = ED1.Person_id
                left join v_pmUser pu2 on pu2.pmUser_id = EC.pmUser_signID
                left join v_TimeTableGraf_lite ttgl on ttgl.TimeTableGraf_id = ED1.TimeTableGraf_id
                left join v_TimeTableMedService_lite ttmsl on ttmsl.TimeTableMedService_id = ED1.TimeTableMedService_id
                
            WHERE (1=1)
                AND EC.EvnConcilium_id is null
                 AND  MST.MedServiceType_SysNick = 'remoteconsultcenter' 
        
            union
        
            SELECT
                ED2.EvnClass_id as "EvnClass_id",
                ED2.Diag_FullName as "Diag_FullName",
                ED2.EvnDirection_id as "EvnDirection_id",
                ED2.EvnDirection_pid as "EvnDirection_pid",
                ED2.EvnDirection_rid as "EvnDirection_rid",
                ED2.r_EvnClass_SysNick as "RootEvnClass_SysNick",
                ED2.EUT_EvnUslugaTelemed_id as "EvnUslugaTelemed_id",
                CF.ConsultingForm_Name as "ConsultingForm_Name",
                ED2.Diag_id as "Diag_id",
                ED2.Person_id as "Person_id",
                ED2.PersonEvn_id as "PersonEvn_id",
                ED2.Server_id as "Server_id",
                ED2.EvnDirection_Num as "EvnDirection_Num",
                ED2.PayType_id as "PayType_id",
                to_char(ED2.EvnDirection_setDT, 'dd.mm.yyyy') as "EvnDirection_setDT",
                to_char(ED2.EvnDirection_insDT, 'dd.mm.yyyy') as "EvnDirection_insDate",
                to_char(ED2.EvnDirection_insDT, 'hh24:MI') as "EvnDirection_insTime",
                v_Lpu.Lpu_Nick as "Lpu_Nick",
                v_Lpu.Lpu_id as "Lpu_id",
                pp.RiskType_id as "RiskType_id",
                mp.Person_Fin as "Person_Fin",
                mp2.Person_Fin as "MedPersonalFIO_whom_sent",
                (PS.Person_SurName || ' ' || Coalesce(PS.Person_FirName, '') || ' ' || Coalesce(PS.Person_SecName,'')) as "Person_FIO",
                case when 2 = Coalesce(ED2.EvnDirection_IsCito,1) then 'true' else 'false' end as "EvnDirection_IsCito",
                RCC.RemoteConsultCause_Name as "RemoteConsultCause_Name",
                ED2.LpuSectionProfile_Name,
                UTRT.UslugaTelemedResultType_Name as "UslugaTelemedResultType_Name",
                consForm.ConsultationForm_Code as "ConsultationForm_Code",
                consForm.ConsultationForm_Name as "ConsultationForm_Name",
                ED2.isDel as "isDel",
                case
                    when consForm.ConsultationForm_Code is null
                        then ''
                    when consForm.ConsultationForm_Code = '1'
                        then to_char(DATEADD('day',14,ED2.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED2.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '2'
                        then to_char(DATEADD('day',1,ED2.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(ED2.EvnDirection_insDT, 'hh24:MI')
                    when consForm.ConsultationForm_Code = '3'
                        then to_char(DATEADD('hour',2,ED2.EvnDirection_insDT), 'dd.mm.yyyy') || ' ' || to_char(dateadd('hour',2,ED2.EvnDirection_insDT), 'hh24:MI')
                end as "deadLine",
                case
                    when consForm.ConsultationForm_Code is null
                        then 99999999
                    when consForm.ConsultationForm_Code = '1'
                        then abs(DATE_PART('day', DATEADD('day',14,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',14,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '2'
                        then abs(DATE_PART('day', DATEADD('day',1,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',1,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                    when consForm.ConsultationForm_Code = '3'
                        then abs(DATE_PART('day', DATEADD('day',2,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP) * 24 +
                                DATE_PART('hour', DATEADD('day',2,ED2.EvnDirection_insDT) - CURRENT_TIMESTAMP))
                end as "deadDiff",
                DT.DirType_Code as "DirType_Code",
                ED2.EvnQueue_id as "EvnQueue_id",
                ED2.EvnQueue_id as "EvnQueue_id",
                ED2.TimeTableStac_id as "TimeTableStac_id",
                ED2.TimeTableGraf_id as "TimeTableGraf_id",
                ED2.TimeTableMedService_id as "TimeTableMedService_id",
                ESC.EvnStatusCause_Name as "EvnStatusCause_Name",
                case
                    when ED2.EvnDirection_failDT is not null
                        then DFT.DirFailType_Name
                    when EC.EvnConcilium_id is not null and ES2.EvnStatus_SysNick in ('Confirmed', 'Posted')
                        then NCR.ConciliumResult_Name
                    else
                        UTRT.UslugaTelemedResultType_Name
                end as "evndirection_result",
                ex.EvnXml_id as "EvnXml_id",
                ex.EvnXml_IsSigned as "EvnXml_IsSigned",
                to_char(ex.EvnXml_signDT, 'dd.mm.yyyy') as "EvnXml_signDT",
                coalesce(pu2.pmUser_Name, pu.pmUser_Name) as "pmUser_signName",
                ED2.RequestConsult_id as "RequestConsult_id",
                EC.EvnConcilium_id as "EvnConcilium_id",
                EC.ConciliumType_id as "ConciliumType_id",
                ED2.MedService_id as "MedService_id",
                LpuD.Org_id as "Org_tid",
                ED2.RemoteConsultCause_id as "RemoteConsultCause_id",
                ED2.RecMethodType_id as "RecMethodType_id",
                ED2.EvnDirection_isAuto as "EvnDirection_isAuto",
                MS.MedService_Nick as "MedService_Nick",
                CASE WHEN COALESCE(PD.PersonDisp_endDate, (select curDate from cte)) >= (select curDate from cte)
                    THEN 1
                    ELSE 2
                END as "isDisp",
                coalesce(EC.EvnConcilium_IsSigned, ex.EvnXml_IsSigned) as "Evn_IsSigned",
                to_char(coalesce(EC.EvnConcilium_signDT, ex.EvnXml_signDT), 'dd.mm.yyyy') as "Evn_signDT",
                ES2.EvnStatus_SysNick as "EvnConciliumStatus_SysNick",
                ES.EvnStatus_SysNick as "EvnDirectionStatus_SysNick",
                1 as "isConciliumMember",
                CR.ConciliumResults_id as "ConciliumResults_id",
                ED2.pmUser_insID as "pmUser_insID",
                ED2.UslugaComplex_did as "UslugaComplex_did",
                
            case
                -- Если направление на консилиум отменено
                when ED2.EvnDirection_failDT is not null
                    then 5
                    -- Если консилиум проведен и (заполнен результат или есть ТМК услуга)
                when ES2.EvnStatus_SysNick in ('Confirmed', 'Posted') and (CR.ConciliumResults_id is not null or
                    ED2.EUT_EvnUslugaTelemed_id is not null)
                    then 4
                else 1
            end as "evndirection_group"
        
                ,case
                when ED2.TimeTableGraf_id is not null then
                    to_char(ttgl.TimeTableGraf_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttgl.TimeTableGraf_begTime, 'hh24:MI')
                else
                    to_char(ttmsl.TimeTableMedService_begTime, 'dd.mm.yyyy') || ' ' || to_char(ttmsl.TimeTableMedService_begTime, 'hh24:MI')
                end as "TimeTable_begTime"
                , ED2.ConsultingForm_id as "ConsultingForm_id"
                
            FROM
                ED2
                left join v_PersonPregnancy pp  on pp.Person_id = ED2.Person_id
                LEFT JOIN ConsultingForm CF  on CF.ConsultingForm_id = ED2.ConsultingForm_id
                LEFT JOIN v_PersonState PS  on PS.Person_id = ED2.Person_id
                left join v_EvnXml ex on ex.Evn_id = ED2.EUT_EvnUslugaTelemed_id
                left join v_pmUser pu on pu.pmUser_id = ex.pmUser_signID
                left join v_UslugaTelemedResultType UTRT  on UTRT.UslugaTelemedResultType_id = ED2.EUT_UslugaTelemedResultType_id
                LEFT JOIN v_RemoteConsultCause RCC  on RCC.RemoteConsultCause_id = ED2.RemoteConsultCause_id
                LEFT JOIN v_Lpu  on v_Lpu.Lpu_id = ED2.Lpu_sid
                LEFT JOIN v_Lpu LpuD on LpuD.Lpu_id = ED2.Lpu_did
                LEFT JOIN v_MedPersonal mp  on mp.MedPersonal_id = ED2.MedPersonal_id and mp.Lpu_id = ED2.Lpu_sid
                LEFT JOIN v_MedPersonal mp2 on mp2.MedPersonal_id = ED2.MedPersonal_did and mp2.Lpu_id = ED2.Lpu_did
                LEFT JOIN v_ConsultationForm consForm on consForm.ConsultationForm_id = ED2.ConsultationForm_id
                LEFT JOIN v_DirType DT on DT.DirType_id = ED2.DirType_id
                LEFT JOIN v_EvnStatus ES  on ES.EvnStatus_id = ED2.EvnStatus_id
                LEFT JOIN v_EvnConcilium EC on EC.EvnConcilium_pid = ED2.EvnDirection_id
                LEFT JOIN v_EvnStatus ES2 on ES2.EvnStatus_id = EC.EvnStatus_id
                LEFT JOIN v_ConciliumResult CR on CR.EvnConcilium_id = EC.EvnConcilium_id
                left join nsi.v_ConciliumResult NCR on NCR.ConciliumResult_id = CR.ConciliumResults_id
                left join v_DirFailType DFT on DFT.DirFailType_id = ED2.EvnDirection_id
                left join v_EvnStatusCause esc on esc.EvnStatusCause_id = ED2.EvnStatusCause_id
                left join v_MedService MS on MS.MedService_id = ED2.MedService_id
                left join v_MedServiceType MST on MST.MedServiceType_id = MS.MedServiceType_id
                left join v_PersonDisp PD on PD.Person_id = ED2.Person_id
                left join v_pmUser pu2 on pu2.pmUser_id = EC.pmUser_signID
                left join v_TimeTableGraf_lite ttgl on ttgl.TimeTableGraf_id = ED2.TimeTableGraf_id
                left join v_TimeTableMedService_lite ttmsl on ttmsl.TimeTableMedService_id = ED2.TimeTableMedService_id
                
            WHERE (1=1)
                AND EC.EvnConcilium_id is not null
                 AND  MST.MedServiceType_SysNick = 'remoteconsultcenter' 
        
                ) as query
                -- end from
            
                ORDER BY
                    -- order by
                        "EvnDirection_setDT" DESC
                    -- end order by
            
            OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
        
