
            select
                EPVK.EvnPrescrVK_id as "EvnPrescrVK_id",
                EPVK.EvnPrescrVK_pid as "EvnPrescrVK_pid",
                EVK.EvnVK_id as "EvnVK_id",
                EVK.EvnVK_IsSigned as "EvnVK_IsSigned",
                to_char(EVK.EvnVK_signDT, 'dd.mm.yyyy') as "EvnVK_signDT",
                pu.pmUser_Name as "pmUser_signName",
                EVK.EvnVK_DecisionVK as "EvnVK_DecisionVK",
                TTMS.TimetableMedService_id as "TimetableMedService_id",
                coalesce(to_char(TTMS.TimetableMedService_begTime, 'dd.mm.yyyy'),'без записи') as "EvnPrescrVK_setDate",
                coalesce(to_char(TTMS.TimetableMedService_begTime, 'HH24:MI'),'б/з')  as "EvnPrescrVK_setTime",
                CTT.CauseTreatmentType_id as "CauseTreatmentType_id",
                CTT.CauseTreatmentType_Name as "CauseTreatmentType_Name",
                coalesce(EPVK.Diag_id, EVK.Diag_id) as "Diag_id",
                case when EPL.EvnPL_id is not null
                    then EPL.EvnPL_NumCard
                    else EPS.EvnPS_NumCard
                end as "EvnVK_NumCard",
                D.Diag_FullName as "Diag_Name",
                PA.Person_id as "Person_id",
                EPVK.PersonEvn_id as "PersonEvn_id",
                PA.Server_id as "Server_id",
                PA.Person_Fio as "Person_Fio",
                to_char(PA.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
                LS.LpuSection_id as "LpuSection_id",
                LS.LpuSection_FullName as "LpuSection_FullName",
                MP.MedPersonal_id as "MedPersonal_id",
                MP.Person_Fio as "MedPerson_Fio",
                to_char(EVK.EvnVK_setDT, 'dd.mm.yyyy') as "EvnVK_setDT",
                EVK.EvnVK_NumProtocol as "EvnVK_NumProtocol",
                ENT.ExpertiseNameType_Name as "ExpertiseNameType_Name",
                coalesce(ExpertiseEventType.ExpertiseEventType_Name,'') as "ExpertiseEventType_Name",
                coalesce(ExpertiseEventType.ExpertiseEventType_Code,-1) as "ExpertiseEventType_Code",
                to_char(coalesce(EPM.EvnPrescrMse_issueDT,EPM.EvnPrescrMse_setDT), 'dd.mm.yyyy') as "EvnPrescrMse_setDT",
                EPM.EvnPrescrMse_id as "EvnPrescrMse_id",
                EPM.EvnPrescrMse_IsSigned as "EvnPrescrMse_IsSigned",
                EPM.EvnPrescrMse_SpravID as "EvnPrescrMse_SpravID",
                to_char(EPM.EvnPrescrMse_signDT, 'dd.mm.yyyy') as "EvnPrescrMse_signDT",
                epldc.EvnPLDispCert_id as "EvnPLDispCert_id",
                epldc.EvnPLDispCert_IsSigned as "EvnPLDispCert_IsSigned",
                case
                    when epldc.EvnPLDispCert_signDT >= epldc.EvnPLDispCert_updDT then 2
                    else null
                end as "EvnPLDispCert_IsSignActual",
                to_char(epldc.EvnPLDispCert_signDT, 'dd.mm.yyyy') as "EvnPLDispCert_signDT",
                case when EM.EvnMse_id is not null
                    then '№' || cast(EM.EvnMse_NumAct as varchar(50)) || ' от ' || to_char(EM.EvnMse_setDT, 'dd.mm.yyyy')
                    else ''
                end as "EvnMse",
                ES.EvnStick_id as "EvnStickBase_id",
                (
                    coalesce(ES.EvnStick_Ser, '')||' '||coalesce(ES.EvnStick_Num, '')||
                    (case when (ES.EvnStick_begDate IS NOT NULL) then ' выдан: '||to_char(ES.EvnStick_begDate, 'dd.mm.yyyy') else '' end)||
                    (case when (ES.EvnStick_endDate IS NOT NULL) then ' по '||to_char(ES.EvnStick_endDate, 'dd.mm.yyyy') else '' end)
                ) as "EvnStick_all",
                EPVK.EvnPrescrVK_LVN as "EvnVK_LVN",
                EPVK.EvnPrescrVK_Note as "EvnVK_Note",
                
            EDH.EvnDirectionHTM_id as "EvnDirectionHTM_id",
            EDH.EvnDirectionHTM_IsSigned as "EvnDirectionHTM_IsSigned",
            to_char(EDH.EvnDirectionHTM_setDT, 'DD.MM.YYYY') as "EvnDirectionHTM_setDT",
            EDSM.EvnDirection_id as "EvnDirectionSM_id",
            EDSM.EvnDirection_IsSigned as "EvnDirectionSM_IsSigned",
            to_char(EDSM.EvnDirection_setDT, 'DD.MM.YYYY') as "EvnDirectionSM_setDT",
            EDSM.Lpu_id as "EvnDirectionSMLpu_id",
            case
                when EDSM.EvnDirection_id is not null then case when EDSM.EvnDirection_isConfirmed = 2 then 'true' else 'false' end
                when EDH.EvnDirectionHTM_id is not null then case when EDH.EvnDirectionHTM_isConfirmed = 2 then 'true' else 'false' end
            end as "EvnDirection_IsConfirmed",
            case
                when EDSM.EvnDirection_id is not null then EDSM.EvnStatus_id
                when EDH.EvnDirectionHTM_id is not null then EDH.EvnStatus_id
            end as "EvnDirectionStatus_id",
            case
                when EDSM.EvnDirection_id is not null then ess.EvnStatus_Name
                when EDH.EvnDirectionHTM_id is not null then esh.EvnStatus_Name
            end as "EvnDirectionStatus_Name",
            case
                when EDSM.EvnDirection_id is not null then dts.DirType_Code
                when EDH.EvnDirectionHTM_id is not null then dth.DirType_Code
            end as "EvnDirectionDirType_Code",
            case
                when EDSM.EvnDirection_id is not null then EDSM.LpuSectionProfile_id
                when EDH.EvnDirectionHTM_id is not null then EDH.LpuSectionProfile_id
            end as "EvnDirectionLpuSectionProfile_id",
            case
                when EDSM.EvnDirection_id is not null then EDSM.DirType_id
                when EDH.EvnDirectionHTM_id is not null then EDH.DirType_id
            end as "EvnDirectionDirType_id",
            case
                when EDSM.EvnDirection_id is not null then EDSM.Lpu_sid
                when EDH.EvnDirectionHTM_id is not null then EDH.Lpu_sid
            end as "EvnDirectionLpu_sid",
        
                EPVK.EvnStatus_id as "EvnStatusVK_id",
                VLVK.VoteListVK_id as "VoteListVK_id",
                VLVK.VoteListVK_isFinished as "VoteListVK_isFinished",
                vek.VoteExpertVK_id as "VoteExpertVK_id",
                case
                    when
                        vek.VoteExpertVK_id is not null or
                        evke.EvnVKExpert_id is not null
                    then 2
                    else null
                end as "isExpert",
                EVK.EvnVK_isAccepted as "EvnVK_isAccepted",
                EPMS.EvnStatus_id as "EvnStatus_id",
                EPMS.EvnStatus_Name as "EvnStatus_Name",
                EPMS.EvnStatus_SysNick as "EvnStatus_SysNick",
                to_char(EPM.EvnPrescrMse_appointDT, 'dd.mm.yyyy') || ' ' || to_char(EPM.EvnPrescrMse_appointDT, 'HH24:MI') as "EvnPrescrMse_appointDT",
                EVK.EvnVK_IsFail as "EvnVK_IsFail",
                EVK.ExpertiseNameType_id as "ExpertiseNameType_id",
                EVK.ExpertiseEventType_id as "ExpertiseEventType_id",
                EPMP.EvnDirection_From as "EvnDirection_From",
                EM.EvnMse_id as "EvnMse_id",
                MS.MedService_id as "EPM_MedService_id",
                MS.MedService_Name as "EPM_MedService_Name",
                CASE
                    WHEN db_EVN.Evn_deleted = 1 OR db_EVN.Evn_deleted IS NULL
                        THEN db_EVN.Evn_IsSigned
                    ELSE -1
                END AS "prescrIsSigned"
            from
                v_EvnPrescrVK EPVK
                left join v_EvnVK EVK on EVK.EvnPrescrVK_id = EPVK.EvnPrescrVK_id

                LEFT JOIN LATERAL (
                    SELECT pmUser_Name
                    FROM v_pmUser pu
                    WHERE pu.pmUser_id = EVK.pmUser_signID
                    LIMIT 1
                ) pu ON true

                LEFT JOIN v_ExpertiseEventType ExpertiseEventType on ExpertiseEventType.ExpertiseEventType_id = EVK.ExpertiseEventType_id
                left join lateral (select * from v_Person_all PA where PA.PersonEvn_id = EPVK.PersonEvn_id and PA.Server_id = EPVK.Server_id limit 1) PA on true

                LEFT JOIN LATERAL (
                    SELECT Diag_FullName
                    FROM v_Diag D
                    WHERE D.Diag_id = Coalesce(EPVK.Diag_id, EVK.Diag_id)
                    LIMIT 1
                ) D ON true

                left join v_LpuSection LS on LS.LpuSection_id = EPVK.LpuSection_sid

                LEFT JOIN LATERAL (
                    SELECT
                        l.Lpu_Nick || coalesce(' / ' || ls.LpuSection_Name, '') || coalesce(' / ' || MSF.Person_Fio, '') as EvnDirection_From
                    FROM
                        v_MedStaffFact MSF
                        INNER JOIN LATERAL (
                            SELECT Lpu_Nick
                            FROM v_Lpu l
                            WHERE l.Lpu_id = coalesce(EPVK.Lpu_gid,ls.Lpu_id,msf.Lpu_id)
                            LIMIT 1
                        ) l on true
                    WHERE
                        MSF.MedPersonal_id = EPVK.MedPersonal_sid
                    LIMIT 1
                ) EPMP ON true

                left join lateral (
                    select
                        MedPersonal_id,
                        Person_Fio
                    from v_MedPersonal
                    where MedPersonal_id = EPVK.MedPersonal_sid
                        and Lpu_id = EPVK.Lpu_id
                    limit 1
                ) MP on true
                left join v_EvnPrescrMse EPMVK on EPMVK.EvnVK_id = EVK.EvnVK_id
                LEFT JOIN LATERAL (
                    SELECT *
                    FROM v_EvnPrescrMse EPM
                    WHERE EPM.EvnPrescrMse_id = Coalesce(EPVK.EvnPrescrMse_id, EPMVK.EvnPrescrMse_id)
                    LIMIT 1
                ) EPM ON true
                left join v_EvnMse EM on EM.EvnPrescrMse_id = EPM.EvnPrescrMse_id
                left join v_EvnStatus EPMS on EPMS.EvnStatus_id = EPM.EvnStatus_id
                left Join v_CauseTreatmentType CTT on CTT.CauseTreatmentType_id = EPVK.CauseTreatmentType_id
                left join v_ExpertiseNameType ENT on ENT.ExpertiseNameType_id = EVK.ExpertiseNameType_id
                left join v_TimetableMedService_lite TTMS on TTMS.TimetableMedService_id = EPVK.TimetableMedService_id
                    --and TTMS.MedService_id = EPVK.MedService_id
                --пока этот джойн не нужен left join v_EvnQueue EQ on EQ.EvnQueue_pid = EPVK.EvnPrescrVK_id and EQ.MedService_id = EPVK.MedService_id
                left join v_EvnPL EPL on EPL.EvnPL_id = EPVK.EvnPrescrVK_pid
                left join v_EvnPS EPS on EPS.EvnPS_id = EPVK.EvnPrescrVK_pid
                LEFT JOIN LATERAL(
                    SELECT *
                    FROM v_EvnStick
                    WHERE EvnStick_id = EPVK.EvnStick_id
                    LIMIT 1
                ) ES ON true
                left join v_EvnDirectionHTM EDH on
                    EDH.EvnDirectionHTM_pid = EVK.EvnVK_id or
                    EDH.EvnDirectionHTM_id = EPVK.EvnDirectionHTM_id
                left join v_EvnDirection EDSM on EDSM.EvnDirection_id = EPVK.EvnDirection_sid
                left join v_DirType dth on dth.DirType_id = EDH.DirType_id
                left join v_DirType dts on dts.DirType_id = EDSM.DirType_id
                left join v_EvnStatus esh on esh.EvnStatus_id = EDH.EvnStatus_id
                left join v_EvnStatus ess on ess.EvnStatus_id = EDSM.EvnStatus_id
                left join v_MedService MS on MS.MedService_id = EPM.MedService_id
                left join v_EvnQueue EQ on EQ.EvnQueue_id = EPVK.EvnQueue_id
                left join v_VoteListVK VLVK on VLVK.EvnPrescrVK_id = EPVK.EvnPrescrVK_id
                left join v_EvnDirection_all EDA on EQ.EvnDirection_id = EDA.EvnDirection_id
                left join dbo.evn db_EVN on db_EVN.Evn_id = EPM.EvnPrescrMse_id

                LEFT JOIN LATERAL (
                    SELECT EvnStatus_id
                    FROM v_EvnDirection_all ED
                    WHERE ED.EvnDirection_id = Coalesce(TTMS.EvnDirection_id, EQ.EvnDirection_id)
                    LIMIT 1
                ) ED ON true

                left join lateral (
                    select VoteExpertVK_id
                    from v_VoteExpertVK vek
                    inner join v_MedServiceMedPersonal msmp on msmp.MedServiceMedPersonal_id = vek.MedServiceMedPersonal_id
                    where
                        vek.VoteListVK_id = VLVK.VoteListVK_id and
                        msmp.MedPersonal_id = '2455'
                    limit 1
                ) vek on true
                left join lateral (
                    select EvnVKExpert_id
                    from v_EvnVKExpert evke
                    inner join v_MedServiceMedPersonal msmp on msmp.MedServiceMedPersonal_id = evke.MedServiceMedPersonal_id
                    where
                        evke.EvnVK_id = EVK.EvnVK_id and
                        msmp.MedPersonal_id = '2455'
                    limit 1
                ) evke on true
                left join lateral (
                    select
                        EvnPLDispCert_id,
                        EvnPLDispCert_IsSigned,
                        EvnPLDispCert_signDT,
                        EvnPLDispCert_updDT
                    from v_EvnPLDispCert
                    where EvnVK_id = EVK.EvnVK_id
                    limit 1
                ) as epldc on true
            where
                (
                    (
                        EVK.EvnVK_setDT is null
                        and cast(coalesce(TTMS.TimetableMedService_begTime, '2023-07-17') as date) >= '2023-07-17'
                        and cast(coalesce(TTMS.TimetableMedService_begTime, '2023-07-28') as date) <= '2023-07-28'
                        and cast(coalesce(EQ.EvnQueue_setDate, EPVK.EvnPrescrVK_insDT) as date)  <= '2023-07-28'
                        and cast(coalesce(EQ.EvnQueue_setDate, EPVK.EvnPrescrVK_insDT) as date)  >= '2023-07-17'
                    )
                    or (
                        EDA.EvnDirection_setDT is not null
                        and cast(coalesce(EDA.EvnDirection_setDT, '2023-07-17') as date) >= '2023-07-17'
                        and cast(coalesce(EDA.EvnDirection_setDT, '2023-07-28') as date) <= '2023-07-28'
                    )
                    or (
                        cast(EVK.EvnVK_setDT as date) >= '2023-07-17'
                        and cast(EVK.EvnVK_setDT as date) <= '2023-07-28'
                    )
                    or (
                        TTMS.TimetableMedService_begTime is not null
                        and cast(coalesce(TTMS.TimetableMedService_begTime, '2023-07-17') as date) >= '2023-07-17'
                        and cast(coalesce(TTMS.TimetableMedService_begTime, '2023-07-28') as date) <= '2023-07-28'
                    )
                )
                and EPVK.MedService_id = '10226'
                and COALESCE(ED.EvnStatus_id, 16) not in (12,13)
                and (
                    NULL is null
                    or EPVK.CauseTreatmentType_id = NULL
                )
                 and PA.Person_SurName ilike 'аникиев'||'%'
        
