
                with
                
                MSF as (
                    select
                        MedPersonal_id,
                        LpuUnitType_SysNick
                    from v_MedStaffFact MSF
                         left join v_LpuUnit LU on MSF.LpuUnit_id = LU.LpuUnit_id
                    where MSF.MedStaffFact_id = '110101000255208'
                )
            , MSFMP as (
            select LpuSection_id
            from v_MedStaffFact
            where MedPersonal_id = (select MedPersonal_id from MSF)
        )
                
            SELECT
                
                case
                    when COALESCE(EvnPS.EvnPS_IsSigned, 1) = 1 and 
            case
                when EvnPS.Lpu_id = '5020' and (
                    EvnPS.LpuSection_pid in (select LpuSection_id from MSFMP) or
                    EvnPS.LpuSection_id in (select LpuSection_id from MSFMP)
                ) then 1
                when (FALSE::boolean = TRUE or FALSE::boolean = TRUE or FALSE::boolean = TRUE) and EvnPS.Lpu_id = '5020' then 1
                else 0
            end = 1
         and COALESCE(EvnPS.LpuSection_pid, 1) <> 1 and COALESCE(EvnPS.MedStaffFact_pid, 1) <> 1 and not exists (
                select RD.Registry_id
                from r11.v_RegistryData RD
                    inner join r11.v_Registry R on R.Registry_id = RD.Registry_id
                    inner join v_RegistryStatus RS on RS.RegistryStatus_id = R.RegistryStatus_id
                where
                    RD.Evn_rid = EvnPS.EvnPS_id
                    and RS.RegistryStatus_SysNick = 'paid'
                limit 1
            ) then 'edit'
                    else 'view'
                end as "accessTypeUsluga"
            ,
                case
                    when COALESCE(EvnPS.EvnPS_IsSigned, 1) = 1 and 
            case
                when EvnPS.Lpu_id = '5020' and (
                    EvnPS.LpuSection_pid in (select LpuSection_id from MSFMP) or
                    EvnPS.LpuSection_id in (select LpuSection_id from MSFMP)
                ) then 1
                when (FALSE::boolean = TRUE or FALSE::boolean = TRUE or FALSE::boolean = TRUE) and EvnPS.Lpu_id = '5020' then 1
                else 0
            end = 1
         then 'edit'
                    else 'view'
                end as "accessTypeAdmission"
            ,
                case
                    when COALESCE(EvnPS.EvnPS_IsSigned, 1) = 1 and 
            case
                when EvnPS.Lpu_id = '5020' and (
                    EvnPS.LpuSection_pid in (select LpuSection_id from MSFMP) or
                    EvnPS.LpuSection_id in (select LpuSection_id from MSFMP)
                ) then 1
                when (FALSE::boolean = TRUE or FALSE::boolean = TRUE or FALSE::boolean = TRUE) and EvnPS.Lpu_id = '5020' then 1
                else 0
            end = 1
         and COALESCE(EvnPS.LpuSection_pid, 1) <> 1 and COALESCE(EvnPS.MedStaffFact_pid, 1) <> 1 then 'edit'
                    else 'view'
                end as "accessType"
            ,
                case
                    when COALESCE(EvnPS.EvnPS_IsSigned, 1) = 1 and 
            case
                when EvnPS.Lpu_id = '5020' and (
                    EvnPS.LpuSection_pid in (select LpuSection_id from MSFMP) or
                    EvnPS.LpuSection_id in (select LpuSection_id from MSFMP)
                ) then 1
                when (FALSE::boolean = TRUE or FALSE::boolean = TRUE or FALSE::boolean = TRUE) and EvnPS.Lpu_id = '5020' then 1
                else 0
            end = 1
         and COALESCE(EvnPS.LpuSection_pid, 1) <> 1 and COALESCE(EvnPS.MedStaffFact_pid, 1) <> 1 then '1'
                    else '0'
                end as "allowUnsign"
            ,
                ESPriem.EvnSection_id as "EvnSection_id",ESPriem.EvnSection_IsSigned as "EvnSection_IsSigned",ESPriem.LeaveType_prmid as "LeaveType_prmid",ESPriem.LeaveType_fedid as "LeaveType_fedid",ESPriem.ResultDeseaseType_fedid as "ResultDeseaseType_fedid",ESPriem.LpuSectionProfile_id as "LpuSectionProfile_id",ESPriem.UslugaComplex_id as "UslugaComplex_id",LT.LeaveType_SysNick as "LeaveTypePrm_SysNick",LT.LeaveType_Code as "LeaveType_Code",(case when vits.PersonVitalParam_BreathFrequency is null then ceps.RepositoryObserv_BreathFrequency else vits.PersonVitalParam_BreathFrequency end) as "PersonVitalParam_BreathFrequency",
                EvnPS.EvnPS_id as "EvnPS_id",
                EvnPS.Person_id as "Person_id",
                dbo.Age2(PS.Person_BirthDay, localtimestamp) as "Person_Age",
                EvnPS.PersonEvn_id as "PersonEvn_id",
                EvnPS.Server_id as "Server_id",
                EvnPS.Lpu_id as "Lpu_id",
                EvnPS.EvnPS_IsPaid as "EvnPS_IsPaid",
                COALESCE(to_char(EvnPS.EvnPS_setDT, 'DD.MM.YYYY'),'') as "EvnPS_setDate",
                COALESCE(to_char(EvnPS.EvnPS_setDT, 'HH24:MI'),'') as "EvnPS_setTime",
                COALESCE(to_char(EvnPS.EvnPS_disDT, 'DD.MM.YYYY'),'') as "EvnPS_disDate",
                COALESCE(to_char(EvnPS.EvnPS_disDT, 'HH24:MI'),'') as "EvnPS_disTime",
                RTRIM(EvnPS.EvnPS_NumCard) as "EvnPS_NumCard",
                case when EvnPS.EvnPS_IsCont = 2 then 1 else null end as "EvnPS_IsCont",-- переведен
                EvnPS.PayType_id as "PayType_id",
                EvnPS.CmpCallCard_id as "CmpCallCard_id",
                EvnPS.EvnPS_CodeConv as "EvnPS_CodeConv",
                EvnPS.EvnPS_NumConv as "EvnPS_NumConv",
                EvnPS.PrehospArrive_id as "PrehospArrive_id",
                EvnPS.LpuSection_pid as "LpuSection_pid",
                EvnPS.MedStaffFact_pid as "MedStaffFact_pid",
                case when EvnPS.EvnPS_IsNeglectedCase = 2 then 1 else null end as "EvnPS_IsNeglectedCase",
                case when EvnPS.EvnPS_IsZNO = 2 then 1 else null end as "EvnPS_IsZNO",
                COALESCE(to_char(EvnPS.EvnPS_OutcomeDT, 'DD.MM.YYYY'),'') as "EvnPS_OutcomeDate",
                COALESCE(to_char(EvnPS.EvnPS_OutcomeDT, 'HH24:MI'),'') as "EvnPS_OutcomeTime",
                EvnPS.LpuSection_eid as "LpuSection_eid",
                EvnPS.PrehospWaifRefuseCause_id as "PrehospWaifRefuseCause_id",
                PWRC.PrehospWaifRefuseCause_Code as "PrehospWaifRefuseCause_Code",
                EvnPS.MedPersonal_tid as "MedPersonal_tid",
                EvnPS.MedPersonal_sid as "MedPersonal_sid",
                MSFPriem.MedPersonal_id as "MedPersonal_pid",
                LSPriem.LpuSection_Name as "LpuSectionPriem_Name",
                MSFPriem.Person_Fio as "MedPersonalPriem_Fio",
                MSFTid.MedStaffFact_id as "MedStaffFact_tid",
                MSFSid.MedStaffFact_id as "MedStaffFact_sid",
                case when EvnPS.EvnPS_DocTrans is not null or EvnPS.Lpu_eid is not null then 1 else null end as "isLeaveTypeOther",
                EvnPS.EvnPS_DocTrans as "EvnPS_DocTrans",
                EvnPS.Lpu_eid as "Lpu_eid",
                EvnPS.MedicalCareFormType_id as "MedicalCareFormType_id",
                EvnPS.ResultClass_id as "ResultClass_id",
                EvnPS.DiagSetPhase_aid as "DiagSetPhase_aid",
                EvnPS.ResultDeseaseType_id as "ResultDeseaseType_id",
                COALESCE(EvnPS.Diag_id, EvnPS.Diag_pid ) as "Diag_id",
                EvnPS.EvnPS_IsSigned as "EvnPS_IsSigned",
                COALESCE(D.Diag_Code,'') as "Diag_Code", -- основной диагноз последнего движения или приемного отделения
                COALESCE(D.Diag_Name,'') as "Diag_Name",
                EvnPS.PrehospDirect_id as "PrehospDirect_id",
                EvnPS.Diag_did as "Diag_did",
                EvnPS.EvnDirection_Num as "EvnDirection_Num",
                COALESCE(to_char(EvnPS.EvnDirection_setDT, 'DD.MM.YYYY'),'') as "EvnDirection_setDate",
                COALESCE(EvnPS.Org_did, LPU_DID.Org_id, EvnPS.OrgMilitary_did, ED.Org_sid) as "Org_did", /* Направившая организация */
                EvnPS.MedStaffFact_did as "MedStaffFact_did",
                EvnPS.MedPersonal_did as "MedPersonal_did",
                EvnPS.MedStaffFact_TFOMSCode as "MedStaffFact_TFOMSCode",
                EvnPS.LpuSection_did as "LpuSection_did",
                COALESCE(EvnPS.Lpu_did, ED.Lpu_sid, ED.Lpu_id) as "Lpu_did", /* Направившая МО */
                EvnPS.EvnDirection_id as "EvnDirection_id",
                EDH.EvnDirectionHTM_id as "EvnDirectionHTM_id",
                ED.DirType_id as "DirType_id",
                COALESCE(ED.EvnDirection_IsAuto,1) as "EvnDirection_IsAuto",
                COALESCE(ED.EvnDirection_IsReceive,1) as "EvnDirection_IsReceive",
                case when EvnPS.EvnPS_IsWithoutDirection = 2 then 1 else null end as "EvnPS_IsWithoutDirection",
                case when EvnPS.EvnPS_IsWithoutDirection = 2 then 0 else 1 end as "EvnPS_IsWithDirection",
                EvnPS.DiagSetPhase_did as "DiagSetPhase_did",
                EvnPS.EvnPS_PhaseDescr_did as "EvnPS_PhaseDescr_did",
                case when EvnPS.EvnPS_IsTransfCall = 2 then 1 else null end as "EvnPS_IsTransfCall",
                case when EvnPS.EvnPS_IsDiagMismatch = 2 then 1 else null end as "EvnPS_IsDiagMismatch",
                case when EvnPS.EvnPS_IsWrongCure = 2 then 1 else null end as "EvnPS_IsWrongCure",
                case when EvnPS.EvnPS_IsShortVolume = 2 then 1 else null end as "EvnPS_IsShortVolume",
                case when EvnPS.EvnPS_IsImperHosp = 2 then 1 else null end as "EvnPS_IsImperHosp",
                case when EvnPS.EvnPS_isMseDirected = 2 then 1 else null end as "EvnPS_isMseDirected",
                PT.PrehospType_SysNick as "PrehospType_SysNick",
                to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
                ecp.EvnCostPrint_IsNoPrint as "EvnCostPrint_IsNoPrint",
                (ecp.EvnCostPrint_Cost::numeric(10,2))::text as "CostPrint",
                EvnUslugaParCnt.cnt as "EvnUslugaParCount",
                EvnStickCnt1.cnt + EvnStickCnt2.cnt as "EvnStickCount",
                EvnReceptCnt.cnt as "EvnReceptCount",
                EvnMediaDataCnt.cnt as "EvnMediaDataCount",
                EvnConsentCnt.cnt as "EvnConsentCount",
                case
                    when dp.Diag_Code IN ('U07.1', 'U07.2') then 3
                    when dd.Diag_Code IN ('U07.1', 'U07.2') then 3
                    when exists(
                        select
                            edps.EvnDiagPS_id
                        from
                            v_EvnDiagPS edps
                            inner join v_Diag d on d.Diag_id = edps.Diag_id
                        where
                            edps.EvnDiagPS_rid = EvnPS.EvnPS_id
                            and edps.DiagSetType_id in (1, 2, 3)
                            and d.Diag_Code IN ('U07.1', 'U07.2')
                        limit 1
                    ) then 3
                    else RepositoryObserv.CovidType_id
                end as "CovidType_id",
                EDPediculos.EvnDiagPSPediculos_id as "EvnDiagPSPediculos_id",
                coalesce(EDPediculos.Pediculos_id, Pediculos.Pediculos_id) as "Pediculos_id",
                coalesce(EDPediculos.Diag_id, Pediculos.Diag_id) as "PediculosDiag_id",
                case
                    when EDPediculos.isSanitation = 2 or Pediculos.Pediculos_isSanitation = 2
                    then 1 else 0 end
                as "Pediculos_isSanitation",
                to_char(
                    coalesce(EDPediculos.SanitationDT, Pediculos.Pediculos_SanitationDT),
                    'dd.mm.yyyy')
                as "Pediculos_Sanitation_setDate",
                to_char(
                    coalesce(EDPediculos.SanitationDT, Pediculos.Pediculos_SanitationDT),
                    'hh24:mi')
                as "Pediculos_Sanitation_setTime",
                case
                    when EDPediculos.Diag_id is not null or Pediculos.Diag_id is not null
                    then 1 else null end
                as "isPediculos",
                EDScabies.EvnDiagPSScabies_id as "EvnDiagPSScabies_id",
                coalesce(EDScabies.Scabies_id, Pediculos.Pediculos_id) as "Scabies_id",
                coalesce(EDScabies.Diag_id, Pediculos.Diag_sid) as "ScabiesDiag_id",
                case
                    when EDScabies.Diag_id is not null or Pediculos.Diag_sid is not null
                    then 1 else null end
                as "isScabies",
                case
                    when EDScabies.isSanitation = 2
                    then 1 else 0 end
                as "Pediculos_isSanitation",
                to_char(
                    coalesce(EDScabies.SanitationDT, Pediculos.Pediculos_SanitationDT),
                    'dd.mm.yyyy')
                as "Scabies_Sanitation_setDate",
                to_char(
                    coalesce(EDScabies.SanitationDT, Pediculos.Pediculos_SanitationDT),
                    'hh24:mi')
                as "Scabies_Sanitation_setTime",
                coalesce(
                    EDPediculos.Pediculos_isPrint,
                    EDScabies.Scabies_isPrint,
                    Pediculos.Pediculos_isPrint,
                    1
                ) as "Pediculos_isPrint",
                EDPediculos.EvnInfectNotify_id as "EvnInfectNotifyPediculos_id",
                EDScabies.EvnInfectNotify_id as "EvnInfectNotifyScabies_id",
                case when DWS.DiagWardStay_id is not null then 1 else null end as "EvnPS_IsDiagWardStay",
                DWS.DiagWardStay_id as "DiagWardStay_id",
                DWS.Evn_id as "DiagWardStayEvn_id",
                DWS.MedStaffFact_id as "DiagWardStayMedStaffFact_pid",
                DWS.DiagWardStay_WardName as "DiagWardStay_WardName",
                DWS.DiagWardStay_Reason as "DiagWardStay_Reason",
                COALESCE(to_char(DWS.DiagWardStay_setDT, 'DD.MM.YYYY'),'') as "DiagWardStay_setDTDate",
                COALESCE(to_char(DWS.DiagWardStay_setDT, 'HH24:MI'),'') as "DiagWardStay_setDTTime",
                ChildLast.ChildLpuSection_id as "ChildLpuSection_id",
                ChildLast.LpuSectionWard_id as "LpuSectionWard_id",
                ChildLast.LpuSectionBedProfileLink_id as "LpuSectionBedProfileLink_id",
                EvnPS.LpuSectionTransType_id as "LpuSectionTransType_id",
                EvnPS.EvnPS_TimeDesease as "EvnPS_TimeDesease",
                EvnPS.Okei_id as "Okei_id",
                EvnPS.PrehospToxic_id as "PrehospToxic_id",
                case when EvnPS.EvnPS_IsUnport = 2 then 1 else null end as "EvnPS_IsUnport",
                case when EvnPS.EvnPS_IsUnlaw = 2 then 1 else null end as "EvnPS_IsUnlaw",
                case when EvnPS.EvnPS_IsActive = 2 then 1 else null end as "EvnPS_IsActive",
                accidentCircumstances.name as "TraumaCircumEvnPS_Name",
                accidentCircumstances.uneditablefield as "TraumaCircumEvnPS_Uneditable",
                EvnPS.PrehospTrauma_id as "PrehospTrauma_id",
                EvnPS.Diag_eid as "Diag_eid",
                COALESCE(to_char(EvnPS.EvnPS_NotificationDT, 'DD.MM.YYYY'),'') as "EvnPS_NotificationDate",
                COALESCE(to_char(EvnPS.EvnPS_NotificationDT, 'HH24:MI'),'') as "EvnPS_NotificationTime",
                EvnPS.EvnPS_Policeman as "EvnPS_Policeman",
                FC.FamilyContact_IsInfoAgree as "FamilyContact_IsInfoAgree",
                FC.FamilyContact_msgDate as "FamilyContact_msgDate",
                FC.FamilyContact_msgTime as "FamilyContact_msgTime",
                case when lenFamilyContact_Phone > 0 then '+7 (' || left(FC.FamilyContact_Phone, 3) || ')' ||
                    (case when lenFamilyContact_Phone > 3 then (' ' || substring(FC.FamilyContact_Phone, 4, 3) ||
                        (case when lenFamilyContact_Phone > 7 then ' ' || substring(FC.FamilyContact_Phone, 7, 2) ||
                            (case when lenFamilyContact_Phone > 9
                                then (' ' || substring(FC.FamilyContact_Phone, 9, lenFamilyContact_Phone - 8))
                            else '' end)
                        else '' end)
                    ) else '' end)
                else FC.FamilyContact_Phone end as "FamilyContact_Phone",
                FC.FamilyContact_FIO as "FamilyContact_FIO",
                EvnPS.MedStaffFact_id as "MedStaffFact_id",
                EvnPS.Diag_pid as "Diag_pid",
                DP.Diag_Code as "DiagPriem_Code",
                EvnPS.DeseaseType_id as "DeseaseType_id",
                EvnPS.DiagSetPhase_pid as "DiagSetPhase_pid",
                EvnPS.EvnPS_PhaseDescr_pid as "EvnPS_PhaseDescr_pid",
                COALESCE(to_char(TCEPS.TraumaCircumEvnPS_setDT, 'DD.MM.YYYY'),'') as "TraumaCircumEvnPS_setDTDate",
                COALESCE(to_char(TCEPS.TraumaCircumEvnPS_setDT, 'HH24:MI'),'') as "TraumaCircumEvnPS_setDTTime",
                COALESCE(to_char(EvnPS.EvnPS_CmpTltDT, 'DD.MM.YYYY'),'') as "EvnPS_CmpTltDate",
                COALESCE(to_char(EvnPS.EvnPS_CmpTltDT, 'HH24:MI'),'') as "EvnPS_CmpTltTime",
                case when EvnPS.EvnPS_IsWaif = 2 then 1 else null end as "EvnPS_IsWaif",
                EvnPS.PrehospWaifArrive_id as "PrehospWaifArrive_id",
                EvnPS.PrehospWaifReason_id as "PrehospWaifReason_id",
                EvnPS.PrehospType_id as "PrehospType_id",
                COALESCE(to_char(ESFIRST.EvnSection_PlanDisDT, 'DD.MM.YYYY'),'') as "EvnSection_PlanDisDT",
                COALESCE(to_char(ESFIRST.EvnSection_PlanDisDT, 'HH24:MI'),'') as "EvnSection_PlanDisTime",
                -- COALESCE(to_char(EvnPS.EvnPS_HTMBegDate, 'DD.MM.YYYY'),'') as "EvnPS_HTMBegDate",
                -- EvnPS.EvnPS_HTMTicketNum as "EvnPS_HTMTicketNum",
                -- COALESCE(to_char(EvnPS.EvnPS_HTMHospDate, 'DD.MM.YYYY'),'') as "EvnPS_HTMHospDate",
                case when EvnPS.EvnPS_CmpTltDT is not null then 1 else null end as "EvnPS_IsCmpTlt",
                EvnPS.Diag_spid as "Diag_spid",
                case when EvnPS.EvnPS_IsPLAmbulance = 2 then 1 else null end as "EvnPS_IsPLAmbulance",
                BSS.PregnancyResult_id as "PregnancyResult_id",
                BSS.AbortMethod_id as "AbortMethod_id",
                PReg.PersonPregnancy_id AS "PersonPregnancy_id",
                DM.DeathMotherType_id as "DeathMotherStatus",
                ceps.CovidType_id as "CovidType_id",
                ceps.DiagConfirmType_id as "DiagConfirmType_id",
                ceps.LungInjuryDegreeType_id as "LungInjuryDegreeType_id",
                ceps.InfectionPlaceSuspect_id as "InfectionPlaceSuspect_id",
                case when ceps.RepositoryObserv_IsStayStac = 2 then null else 1 end as "RepositoryObserv_IsStayStac",
                (case when vits.PersonVitalParam_DiastolPress is null then ceps.RepositoryObserv_Diastolic else vits.PersonVitalParam_DiastolPress end) as "PersonVitalParam_DiastolPress",
                vits.PersonVitalParam_HeartFrequency as "PersonVitalParam_HeartFrequency",
                (case when vits.PersonVitalParam_SistolPress is null then ceps.RepositoryObserv_Systolic else vits.PersonVitalParam_SistolPress end) as "PersonVitalParam_SistolPress",
                (case when vits.PersonVitalParam_Pulse is null then ceps.RepositoryObserv_Pulse else vits.PersonVitalParam_Pulse end) as "PersonVitalParam_Pulse",
                (case when vits.PersonVitalParam_Temperature is null then ceps.RepositoryObserv_TemperatureFrom else vits.PersonVitalParam_Temperature end) as "PersonVitalParam_Temperature",
                (case when ceps.RepositoryObserv_Height is null then RepositoryHeight.Value else ceps.RepositoryObserv_Height end) as "PersonHeight_Height",
                (case when ceps.RepositoryObserv_Weight is null then RepositoryWeight.Value else ceps.RepositoryObserv_Weight end) as "PersonWeight_Weight",
                COALESCE(to_char(ceps.RepositoryObserv_FluorographyDate, 'DD.MM.YYYY'),'') as "RepositoryObserv_FluorographyDate",
                ceps.RepositoryObserv_SpO2 as "RepositoryObserv_SpO2",
                RoutingLevel.RoutingLevel_id as "RoutingLevel_id",
                ESLAST.MedStaffFact_id as "sectionMedStaffFact_id",
                CCC.CmpCallCard_EmgTeamComm as "CmpCallCard_EmgTeamComm",
                EvnPS.EvnPS_HospCount as "EvnPS_HospCount",
                PA.PrehospArrive_SysNick as "PrehospArrive_SysNick",
                EvnPSDiag.Diag_Code as "EvnPSDiag_Code",
                PDE.PayContract_id as "PayContract_id",
                PDE.PolisDMS_id as "PolisDMS_id",
                qa.isQualityAnketa as "isQualityAnketa",
                CCC.CmpCallCard_UAddress as "CmpCallCard_UAddress"
            FROM
                v_EvnPS EvnPS
                
                inner join v_EvnSection as ESPriem on ESPriem.EvnSection_pid = EvnPS.EvnPS_id
                    and ESPriem.EvnSection_IsPriem = 2
                left join v_LeaveType LT on LT.LeaveType_id = ESPriem.LeaveType_prmid
            
                left join v_EvnCostPrint ecp on ecp.Evn_id = EvnPS.EvnPS_id
                left join dbo.v_CmpCallCard CCC on CCC.CmpCallCard_id = EvnPS.CmpCallCard_id
                left join v_Diag DD on EvnPS.Diag_did = DD.Diag_id
                left join v_Diag DP on EvnPS.Diag_pid = DP.Diag_id
                left join v_Diag EvnPSDiag on EvnPSDiag.Diag_id = coalesce(EvnPS.Diag_id, EvnPS.Diag_did, EvnPS.Diag_pid)
                left join v_PrehospType PT on EvnPS.PrehospType_id = PT.PrehospType_id
                left join v_PrehospArrive PA on EvnPS.PrehospArrive_id = PA.PrehospArrive_id
                left join v_PrehospWaifRefuseCause PWRC on PWRC.PrehospWaifRefuseCause_id = EvnPS.PrehospWaifRefuseCause_id
                left join v_EvnSection ESLAST on ESLAST.EvnSection_id = EvnPS.EvnSection_lastid
                left join v_EvnSection ESFIRST on ESFIRST.EvnSection_id = EvnPS.EvnSection_firstid
                left join v_Diag D on COALESCE(ESLAST.Diag_id, EvnPS.Diag_pid) = D.Diag_id
                left join v_Org OP on EvnPS.EvnPS_IsCont = 2 AND EvnPS.Org_did = OP.Org_id
                left join v_LpuSection LSPriem on LSPriem.LpuSection_id = EvnPS.LpuSection_pid
                left join v_LpuSection LSP on EvnPS.EvnPS_IsCont = 2 AND EvnPS.LpuSection_did = LSP.LpuSection_id
                left join v_Org OD on EvnPS.Org_did = OD.Org_id
                left join v_MedStaffFact MSFPriem on MSFPriem.MedStaffFact_id = EvnPS.MedStaffFact_pid
                left join v_LpuSection LSD on EvnPS.EvnDirection_id is not null AND EvnPS.LpuSection_did = LSD.LpuSection_id
                left join v_PersonState PS on EvnPS.Person_id = PS.Person_id
                LEFT JOIN LATERAL (
                    select
                        PersonRegister_id,
                        PersonPregnancy_id
                    from
                        v_PersonPregnancy
                    where Person_id = EvnPS.Person_id
                    order by
                        PersonPregnancy_id
                    desc
                    limit 1
                ) PReg on true
                LEFT JOIN LATERAL (
                    select
                        PersonRegister_id
                    from
                        v_PersonRegister
                    where Person_id = EvnPS.Person_id and PersonRegisterType_id = 2
                    order by
                        PersonRegister_id
                    desc
                    limit 1
                ) PersonRegister on true
                LEFT JOIN LATERAL(
                    select
                        DeathMotherType_id
                    from v_DeathMother
                    where PersonRegister_id = coalesce(PReg.PersonRegister_id,PersonRegister.PersonRegister_id)
                    ORDER BY DeathMother_id DESC
                    LIMIT 1
                ) DM on true
                left join v_BirthSpecStac BSS on BSS.PersonRegister_id = coalesce(PReg.PersonRegister_id,PersonRegister.PersonRegister_id)
                left join v_EvnDirection_all ED on ED.EvnDirection_id = EvnPS.EvnDirection_id
                left join v_EvnDirectionHTM EDH on EDH.EvnDirectionHTM_id = EvnPS.EvnDirection_id
                left join v_Lpu LPU_DID on LPU_DID.Lpu_id = EvnPS.Lpu_did
                left join v_DiagWardStay DWS on EvnPS.EvnPS_id = DWS.Evn_id
                left join lateral (
                    select
                        LpuSection_id as ChildLpuSection_id,
                        LpuSectionWard_id as LpuSectionWard_id,
                        LpuSectionBedProfileLink_fedid as LpuSectionBedProfileLink_id
                    from
                        v_EvnSection
                    where
                        EvnSection_id = EvnPS.EvnSection_lastid
                    limit 1
                ) as ChildLast on true
                left join lateral (
                    select CovidType_id
                    from v_RepositoryObserv
                    where Evn_id = '110101364624675'
                    order by RepositoryObserv_updDT DESC
                    limit 1
                ) as RepositoryObserv on true
                left join lateral (
                    select PersonHeight_Height as Value
                    from v_PersonHeight
                    where Person_id = EvnPS.Person_id
                    order by PersonHeight_updDT DESC
                    limit 1
                ) as RepositoryHeight on true
                left join lateral (
                    select
                        case when Okei_id = 36 then PersonWeight_Weight/1000 else PersonWeight_Weight end as Value
                    from v_PersonWeight
                    where Person_id = EvnPS.Person_id
                    order by PersonWeight_updDT DESC
                    limit 1
                ) as RepositoryWeight on true
                left join v_TraumaCircumEvnPS tceps on tceps.EvnPS_id = EvnPS.EvnPS_id
                left join lateral (
                    select distinct coalesce(ccd.localize, CCC.cmpcallcard_accidentcircumstances) as name,
                                    case when CCC.cmpcallcard_accidentcircumstances is not null then 'true' else 'false' end as uneditablefield,
                                    cclsc.cmpclosecard_upddt
                    from dbo.cmpclosecard cclsc
                             inner join dbo.cmpclosecardrel ccd on cclsc.cmpclosecard_id = ccd.cmpclosecard_id
                             inner join dbo.cmpclosecardcombo cccc on cccc.cmpclosecardcombo_id = ccd.cmpclosecardcombo_id
                    where (cclsc.cmpcallcard_id = CCC.CmpCallCard_id) -- Обстоятельства получения травмы
                ) accidentCircumstances on true
                left join lateral (
                    select
                        FC.EvnPS_id,
                        length(FC.FamilyContact_Phone) as lenFamilyContact_Phone,
                        FC.FamilyContact_Phone,
                        FC.FamilyContact_FIO,
                        case when FC.FamilyContact_IsInfoAgree = 2 then 1 else 0 end as FamilyContact_IsInfoAgree,
                        COALESCE(to_char(FC.FamilyContact_msgDT, 'DD.MM.YYYY'), '') as FamilyContact_msgDate,
                        COALESCE(to_char(FC.FamilyContact_msgDT, 'HH24:MI'),'') as FamilyContact_msgTime
                    from v_FamilyContact FC
                    where FC.EvnPS_id = EvnPS.EvnPS_id
                ) as FC on true
                left join lateral (
                    select
                        ceps.CovidType_id,
                        ceps.DiagConfirmType_id,
                        ceps.LungInjuryDegreeType_id,
                        ceps.InfectionPlaceSuspect_id,
                        ceps.RepositoryObserv_IsStayStac,
                        ceps.RepositoryObserv_BreathRate,
                        ceps.RepositoryObserv_BreathFrequency,
                        ceps.RepositoryObserv_Systolic,
                        ceps.RepositoryObserv_Diastolic,
                        ceps.RepositoryObserv_Height,
                        ceps.RepositoryObserv_Weight,
                        ceps.RepositoryObserv_TemperatureFrom,
                        ceps.RepositoryObserv_FluorographyDate,
                        ceps.RepositoryObserv_SpO2,
                        ceps.RepositoryObserv_Pulse
                    from
                        v_RepositoryObserv ceps
                    where
                        ceps.Evn_id = EvnPS.EvnPS_id
                    ORDER BY
                        ceps.RepositoryObserv_updDT DESC
                    limit 1
                ) as ceps on true
                left join lateral (
                    select
                        vits.PersonVitalParam_HeartFrequency,
                        vits.PersonVitalParam_BreathFrequency,
                        vits.PersonVitalParam_SistolPress,
                        vits.PersonVitalParam_DiastolPress,
                        vits.PersonVitalParam_Temperature,
                        vits.PersonVitalParam_Pulse
                    from
                        v_PersonVitalParam vits
                    where
                        vits.Evn_id = EvnPS.EvnPS_id
                    ORDER BY
                        vits.PersonVitalParam_updDT DESC
                    limit 1
                ) as vits on true
                left join lateral (
                    select *
                    FROM v_Pediculos P
                    where P.Evn_id = EvnPS.EvnPS_id
                    order by P.Pediculos_insDT desc
                    limit 1
                ) as Pediculos on true
                left join lateral (
                    select
                        d.EvnDiagPS_id as EvnDiagPSPediculos_id,
                        ein.EvnInfectNotify_id as EvnInfectNotify_id,
                        p.Pediculos_id as Pediculos_id,
                        p.Pediculos_isPrint as Pediculos_isPrint,
                        p.Diag_id as Diag_id,
                        p.Pediculos_DiagDT as DiagDT,
                        case when p.Pediculos_SanitationDT is not null then 1 else 0 end as isSanitation,
                        p.Pediculos_SanitationDT as SanitationDT
                    from v_EvnDiagPS d
                        inner join v_Pediculos p on p.Evn_id = d.EvnDiagPS_id and p.Diag_id is not null
                        left join v_EvnInfectNotify ein on EvnInfectNotify_pid = d.EvnDiagPS_id
                    where
                        EvnDiagPS_pid = ESPriem.EvnSection_id
                    order by EvnDiagPS_id desc
                    limit 1
                ) EDPediculos on true
                left join lateral (
                    select
                        d.EvnDiagPS_id as EvnDiagPSScabies_id,
                        ein.EvnInfectNotify_id as EvnInfectNotify_id,
                        p.Pediculos_id as Scabies_id,
                        p.Pediculos_isPrint as Scabies_isPrint,
                        p.Diag_sid as Diag_id,
                        p.Pediculos_ScabiesDT as DiagDT,
                        case when p.Pediculos_SanitationScabiesDT is not null then 1 else 0 end as isSanitation,
                        p.Pediculos_SanitationScabiesDT as SanitationDT
                    from v_EvnDiagPS d
                        inner join v_Pediculos p on p.Evn_id = d.EvnDiagPS_id and p.Diag_sid is not null
                        left join v_EvnInfectNotify ein on EvnInfectNotify_pid = d.EvnDiagPS_id
                    where
                        EvnDiagPS_pid = ESPriem.EvnSection_id
                    order by EvnDiagPS_id desc
                    limit 1
                ) EDScabies on true
                left join lateral (
                    select
                        count(EUP.EvnUslugaPar_id) as cnt
                    from
                        v_EvnUslugaPar EUP
                        left join v_EvnUslugaPar EvnUP on EvnUP.EvnUslugaPar_id = eup.EvnUslugaPar_pid
                    where
                        EUP.EvnUslugaPar_rid = EvnPS.EvnPS_id
                        and EUP.EvnUslugaPar_setDT is not null
                        and EvnUP.EvnUslugaPar_id is null
                ) as EvnUslugaParCnt on true
                left join lateral (
                    select
                        count(EvnRecept_id) as cnt
                    from
                        v_EvnRecept
                    where
                        EvnRecept_pid = EvnPS.EvnPS_id
                        and Lpu_id = EvnPS.Lpu_id
                ) as EvnReceptCnt on true
                left join lateral (
                    select
                        count(EvnStickBase_id) as cnt
                    from
                        v_EvnStickBase
                    where
                        EvnStickBase_mid = EvnPS.EvnPS_id
                ) as EvnStickCnt1 on true
                left join lateral (
                    select
                        count(ES.EvnStickBase_id) as cnt
                    from
                        v_EvnStickBase ES
                    where
                        exists (select EvnLink_id from v_EvnLink where Evn_id = EvnPS.EvnPS_id and Evn_lid = ES.EvnStickBase_id)
                ) as EvnStickCnt2 on true
                left join lateral (
                    select
                        count(EvnMediaData_id) as cnt
                    from
                        v_EvnMediaData
                    where
                        Evn_id = EvnPS.EvnPS_id
                ) as EvnMediaDataCnt on true
                left join lateral (
                    select
                        count(CON.Consent_id) as cnt
                    from
                        v_Consent CON
                    LEFT JOIN
                        v_Evn E ON CON.Evn_id = E.Evn_id
                    where
                        E.Evn_rid = EvnPS.EvnPS_id
                        and CON.Lpu_id = EvnPS.Lpu_id
                ) EvnConsentCnt on true
                left join lateral (
                    select t1.MedStaffFact_id
                    from v_MedStaffFact t1
                        inner join v_LpuSection t2 on t2.LpuSection_id = t1.LpuSection_id
                        inner join v_LpuUnit t3 on t3.LpuUnit_id = t2.LpuUnit_id
                    where
                        t1.Lpu_id = EvnPS.Lpu_id
                        and t1.MedPersonal_id = EvnPS.MedPersonal_tid
                        and EvnPS.EvnPS_OutcomeDT is not null
                        and (t1.WorkData_begDate is null or t1.WorkData_begDate <= EvnPS.EvnPS_OutcomeDT)
                        and (t1.WorkData_endDate is null or t1.WorkData_endDate >= EvnPS.EvnPS_OutcomeDT)
                        and t3.LpuUnitType_id in (1, 6, 7, 9, 17)
                        and t1.PostKind_id is not null
                    order by t1.PostKind_id asc nulls last
                    limit 1
                ) as MSFTid on true
                left join lateral (
                    select t1.MedStaffFact_id
                    from v_MedStaffFact t1
                        inner join v_LpuSection t2 on t2.LpuSection_id = t1.LpuSection_id
                        inner join v_LpuUnit t3 on t3.LpuUnit_id = t2.LpuUnit_id
                    where
                        t1.Lpu_id = EvnPS.Lpu_id
                        and t1.MedPersonal_id = EvnPS.MedPersonal_sid
                        and EvnPS.EvnPS_OutcomeDT is not null
                        and (t1.WorkData_begDate is null or t1.WorkData_begDate <= EvnPS.EvnPS_OutcomeDT)
                        and (t1.WorkData_endDate is null or t1.WorkData_endDate >= EvnPS.EvnPS_OutcomeDT)
                        and t3.LpuUnitType_id in (1, 6, 7, 9, 17)
                        and t1.PostKind_id is not null
                    order by t1.PostKind_id asc nulls last
                    limit 1
                ) as MSFSid on true
                left join lateral (
                    select
                        RM.RoutingLevel_id
                    from
                        v_RoutingMap RM
                        inner join v_RoutingProfile RP on RP.RoutingProfile_id = RM.RoutingProfile_id
                        and RoutingProfile_sysnick = 'oks'
                    where
                        RM.Lpu_id = EvnPS.Lpu_id
                        and RM.RoutingMap_endDate is null
                        and RP.RoutingProfile_endDate is null
                    order by
                        RM.RoutingLevel_id
                    limit 1
                ) RoutingLevel on true
                left join lateral (
                    select
                        PDE.PayContract_id,
                        PDE.PolisDMS_id
                    from v_PayDocEvn PDE
                    where PDE.Evn_id = EvnPS.EvnPS_id
                    limit 1
                ) PDE on true
                left join lateral (
                    select count(QualityReview_id) as isQualityAnketa
                    from v_QualityReview
                    where Evn_id = EvnPS.EvnPS_id limit 1
                ) qa on true
            WHERE
                EvnPS.EvnPS_id = '110101364624675'
            limit 1
        
            
