
            select
                 COALESCE(EPS.EvnPS_NumCard, '') as "EvnPS_NumCard"
                ,RTRIM(COALESCE(Lpu.Lpu_Name, '')) as "Lpu_Name"
                ,RTRIM(COALESCE(PLST.PolisType_Name, '')) as "PolisType_Name"
                ,CASE WHEN PLST.PolisType_Code = 4 then '' ELSE RTRIM(COALESCE(PLS.Polis_Ser, '')) END as "Polis_Ser"
                ,CASE WHEN PLST.PolisType_Code = 4 then COALESCE(RTRIM(PS.Person_EdNum), '') ELSE RTRIM(COALESCE(PLS.Polis_Num, '')) END AS "Polis_Num"
                --,RTRIM(COALESCE(PLS.Polis_Num, '')) as "Polis_Num"
                --,RTRIM(COALESCE(PLS.Polis_Ser, '')) as "Polis_Ser"
                ,COALESCE('код терр. ' || cast(OST.OMSSprTerr_Code as varchar(5)), '') as "OMSSprTerr_Code"
                ,COALESCE('выдан ' || OrgSmo.OrgSMO_Nick, '') as "OrgSmo_Name"
                ,COALESCE(OS.Org_OKATO, '') as "OrgSmo_OKATO"
                ,PS.Person_id as "Person_id"
                ,RTRIM(RTRIM(COALESCE(PS.Person_Surname, '')) || ' ' || RTRIM(COALESCE(PS.Person_Firname, '')) || ' ' || RTRIM(COALESCE(PS.Person_Secname, ''))) as "Person_Fio"
                ,LEFT(COALESCE(SX.Sex_Name, ''), 3) as "Sex_Name"
                ,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
                ,dbo.Age2(PS.Person_Birthday, EPS.EvnPS_setDT) as "Person_AgeYears"
                ,RTRIM(COALESCE(D.Document_Num, '')) as "Document_Num"
                ,RTRIM(COALESCE(D.Document_Ser, '')) as "Document_Ser"
                ,RTRIM(COALESCE(DT.DocumentType_Name, '')) as "DocumentType_Name"
                ,RTRIM(COALESCE(KLAT.KLAreaType_Name, '')) as "KLAreaType_Name"
                ,RTRIM(COALESCE(CAST(KLAT.KLAreaType_id as varchar), '')) as "KLAreaType_id"
                ,RTRIM(COALESCE(PS.Person_Phone, '')) as "Person_Phone"
                ,RTRIM(COALESCE(PAddr.Address_Address, '')) as "PAddress_Name"
                ,RTRIM(COALESCE(UAddr.Address_Address, '')) as "UAddress_Name"
                ,COALESCE(MSF.MedPersonal_TabCode, '') as "MedPersonalPriem_Code"
                ,COALESCE(MSF.Person_Fio, '') as "MedPersonalPriem_FIO"
                ,RTRIM(COALESCE(PT.PayType_Name, '')) as "PayType_Name"
                ,RTRIM(COALESCE(CAST(PT.PayType_Code as varchar), '')) as "PayType_Code"
                ,RTRIM(COALESCE(SS.SocStatus_Name, '')) as "SocStatus_Name"
                ,RTRIM(COALESCE(SS.SocStatus_Code, '')) as "SocStatus_Code"
                ,RTRIM(COALESCE(SS.SocStatus_SysNick, '')) as "SocStatus_SysNick"
                ,IT.PrivilegeType_id as "PrivilegeType_id"
                ,COALESCE(CAST(IT2.PrivilegeType_Code as varchar), '') as "PrivilegeType_Code"
                ,CASE
                    WHEN street.KLStreet_id is not null and street.KLAdr_Ocatd is not null THEN street.KLAdr_Ocatd
                    WHEN town.KLArea_id is not null and town.KLAdr_Ocatd is not null THEN town.KLAdr_Ocatd
                    WHEN city.KLArea_id is not null and city.KLAdr_Ocatd is not null THEN city.KLAdr_Ocatd
                    WHEN srgn.KLArea_id is not null and srgn.KLAdr_Ocatd is not null THEN srgn.KLAdr_Ocatd
                    WHEN rgn.KLArea_id is not null and rgn.KLAdr_Ocatd is not null THEN rgn.KLAdr_Ocatd
                    WHEN country.KLArea_id is not null and country.KLAdr_Ocatd is not null THEN country.KLAdr_Ocatd
                    ELSE ''
                END as "Person_OKATO"
                ,RTRIM(COALESCE(PHLS.LpuSection_Name, PreHospLpu.Lpu_Name, PHOM.OrgMilitary_Name, PHO.Org_Name,PD.PrehospDirect_Name, '')) as "PrehospOrg_Name"
                ,RTRIM(COALESCE(PA.PrehospArrive_Name, '')) as "PrehospArrive_Name"
                ,COALESCE(DiagH.Diag_Code, '') as "PrehospDiag_Code"
                ,COALESCE(DiagH.Diag_Name, '') as "PrehospDiag_Name"
                ,COALESCE(DiagP.Diag_Code, '') as "AdmitDiag_Code"
                ,RTRIM(COALESCE(PHTX.PrehospToxic_Name, '')) as "PrehospToxic_Name"
                ,RTRIM(COALESCE(CAST(PHTX.PrehospToxic_Code as varchar), '')) as "PrehospToxic_Code"
                ,RTRIM(COALESCE(LSTT.LpuSectionTransType_Name, '')) as "LpuSectionTransType_Name"
                ,RTRIM(COALESCE(CAST(LSTT.LpuSectionTransType_Code as varchar), '')) as "LpuSectionTransType_Code"
                ,case
                    when PHT.PrehospType_Code is null then null
                    when PHT.PrehospType_Code = 1 then 4
                    else 3
                 end as "PrehospType_Code"
                ,case
                    when PHT.PrehospType_Code is null then ''
                    when PHT.PrehospType_Code = 1 then 'в плановом порядке'
                    else 'по экстренным показаниям'
                 end as "PrehospType_Name"
                ,case when COALESCE(EPS.EvnPS_HospCount, 1) > 1 then 2 else 1 end as "EvnPS_HospCountCode"
                ,case when COALESCE(EPS.EvnPS_HospCount, 1) > 1 then 'повторно' else 'первично' end as "EvnPS_HospCountName"
                ,EPS.EvnPS_TimeDesease as "EvnPS_TimeDesease"
                ,COALESCE(ED.EvnDirection_Num, EPS.EvnDirection_Num) as "EvnDirection_Num"
                ,to_char(COALESCE(ED.EvnDirection_setDT, EPS.EvnDirection_setDT), 'DD.MM.YYYY') as "EvnDirection_setDate"
                ,EPS.EvnPS_CodeConv as "EvnPS_CodeConv"
                ,EPS.EvnPS_NumConv as "EvnPS_NumConv"
                ,to_char(EPS.EvnDirection_SetDT, 'DD.MM.YYYY') as "EvnDirection_SetDT"
                ,RTRIM(PC.PersonCard_Code) as "PersonCard_Code"
                ,RTRIM(COALESCE(PHTR.PrehospTrauma_Name, '')) as "PrehospTrauma_Name"
                ,RTRIM(COALESCE(CAST(PHTR.PrehospTrauma_Code as varchar), '')) as "PrehospTrauma_Code"
                ,to_char(EPS.EvnPS_setDate, 'DD.MM.YYYY') as "EvnPS_setDate"
                ,EPS.EvnPS_setTime as "EvnPS_setTime"
                ,COALESCE(LSFirst.LpuSection_Code, '') as "LpuSectionFirst_Code"
                ,COALESCE(LSFirst.LpuSection_Name, '') as "LpuSectionFirst_Name"
                ,RTRIM(COALESCE(LSBPFirst.LpuSectionBedProfile_Name, '')) as "LpuSectionBedProfile_Name"
                ,RTRIM(COALESCE(MPFirst.MedPersonal_TabCode, '')) as "MedPersonal_TabCode"
                ,RTRIM(COALESCE(MPFirst.MedPersonal_Code, '')) as "MPFirst_Code"
                ,RTRIM(COALESCE(MPFirst.Person_Fio, '')) as "MedPerson_FIO"
                ,RTRIM(COALESCE(OHMP.Person_Fio,'')) as "OrgHead_FIO"
                ,RTRIM(COALESCE(OHMP.MedPersonal_TabCode,'')) as "OrgHead_Code"
                ,to_char(ESFirst.EvnSection_setDT, 'DD.MM.YYYY') as "EvnSectionFirst_setDate"
                ,ESFirst.EvnSection_setTime as "EvnSectionFirst_setTime"
                ,to_char(EPS.EvnPS_disDate, 'DD.MM.YYYY') as "EvnPS_disDate"
                ,EPS.EvnPS_disTime as "EvnPS_disTime"
                ,LUTLast.LpuUnitType_SysNick as "LpuUnitType_SysNick"
                ,case when LUTLast.LpuUnitType_SysNick = 'stac'
                    then datediff('day', EPS.EvnPS_setDate, EPS.EvnPS_disDate) + abs(sign(datediff('day', EPS.EvnPS_setDate, EPS.EvnPS_disDate)) - 1) -- круглосуточные
                    else (datediff('day', EPS.EvnPS_setDate, EPS.EvnPS_disDate) + 1) -- дневные
                 end - COALESCE(EPSAbsence.EvnPS_Absence, 0) as "EvnPS_KoikoDni"
                ,RTRIM(COALESCE(LT.LeaveType_Name, '')) as "LeaveType_Name"
                ,LT.LeaveType_Code as "LeaveType_Code"
                ,LT.LeaveType_SysNick as "LeaveType_SysNick"
                ,RTRIM(COALESCE(RD.ResultDesease_Name, '')) as "ResultDesease_Name"
                ,RD.ResultDesease_Code as "ResultDesease_Code"
                ,RD.ResultDesease_Code as "ResultDesease_sCode"
                ,case
                    when LT.LeaveType_SysNick in ('die','ksdie','dsdie') then 6
                    when RD.ResultDesease_SysNick in ('kszdor','dszdor') then 1
                    when RD.ResultDesease_SysNick in ('dsuluc','ksuluc') then 2
                    when RD.ResultDesease_SysNick in ('dsbper','ksbper','noteff') then 3
                    when RD.ResultDesease_SysNick in ('dsuchud','ksuchud') then 4
                    when RD.ResultDesease_SysNick in ('dszdor','kszdor') then 5
                    else null
                end as "ResultDesease_aCode"
                ,to_char(EST.EvnStick_setDT, 'DD.MM.YYYY') as "EvnStick_setDate"
                ,to_char(EST.EvnStick_disDT, 'DD.MM.YYYY') as "EvnStick_disDate"
                ,ESTCP.Person_Age as "PersonCare_Age"
                ,ESTCP.Sex_Name as "PersonCare_SexName"
                ,ESTCP.Sex_id as "PersonCare_SexId"
                ,DG.Diag_Code as "LeaveDiag_Code"
                ,DG.Diag_Name as "LeaveDiag_Name"
                ,DGA.Diag_Code as "LeaveDiagAgg_Code"
                ,DGA.Diag_Name as "LeaveDiagAgg_Name"
                ,DGS.Diag_Code as "LeaveDiagSop_Code"
                ,DGS.Diag_Name as "LeaveDiagSop_Name"
                ,PAD.Diag_Code as "AnatomDiag_Code"
                ,PAD.Diag_Name as "AnatomDiag_Name"
                ,PADA.Diag_Code as "AnatomDiagAgg_Code"
                ,PADA.Diag_Name as "AnatomDiagAgg_Name"
                ,PADS.Diag_Code as "AnatomDiagSop_Code"
                ,PADS.Diag_Name as "AnatomDiagSop_Name"
                ,case when EPS.EvnPS_IsImperHosp = 2 then '1; ' else null end as "EvnPS_IsImperHosp"
                ,case when EPS.EvnPS_IsShortVolume = 2 then '2; ' else null end as "EvnPS_IsShortVolume"
                ,case when EPS.EvnPS_IsWrongCure = 2 then '3; ' else null end as "EvnPS_IsWrongCure"
                ,case when EPS.EvnPS_IsDiagMismatch = 2 then '4; ' else null end as "EvnPS_IsDiagMismatch"
                ,LC.LeaveCause_Code as "LeaveCause_Code"
                ,EPS.PrehospWaifRefuseCause_id as "PrehospWaifRefuseCause_id"
                ,IsRW.YesNo_Name as "IsRW"
                ,IsAIDS.YesNo_Name as "IsAIDS"
                ,PEH.PersonEncrypHIV_Encryp as "PersonEncrypHIV_Encryp"
            from dbo.v_EvnPS as EPS
                inner join dbo.v_Lpu as Lpu on Lpu.Lpu_id = EPS.Lpu_id
                inner join dbo.v_PersonState as PS on PS.Person_id = EPS.Person_id
                left join dbo.v_EvnDirection as ED on ED.EvnDirection_id = EPS.EvnDirection_id
                left join v_EvnSection ESLast on ESLast.EvnSection_id = EPS.EvnSection_lastid
                left join v_EvnSection ESFirst on ESFirst.EvnSection_id = EPS.EvnSection_firstid
                left join lateral (
                    select sum(EvnSection_Absence) as EvnPS_Absence
                    from v_EvnSection
                    where EvnSection_pid = EPS.EvnPS_id
                ) as EPSAbsence on true
                left join dbo.v_LpuSection as LSLast on LSLast.LpuSection_id = ESLast.LpuSection_id
                left join dbo.LpuUnit as LULast on LULast.LpuUnit_id = LSLast.LpuUnit_id
                left join dbo.LpuUnitType as LUTLast on LUTLast.LpuUnitType_id = LULast.LpuUnitType_id
                left join dbo.v_PrehospDirect as PD on EPS.PrehospDirect_id = PD.PrehospDirect_id
                left join dbo.v_EvnLeave as ELeave on ELeave.EvnLeave_pid = ESLast.EvnSection_id
                left join dbo.LeaveCause as LC on LC.LeaveCause_id = ELeave.LeaveCause_id
                left join dbo.v_Polis as PLS on PLS.Polis_id = PS.Polis_id
                left join dbo.v_OmsSprTerr as OST on OST.OmsSprTerr_id = PLS.OmsSprTerr_id
                left join dbo.v_PolisType as PLST on PLST.PolisType_id = PLS.PolisType_id
                left join dbo.v_OrgSmo as OrgSmo on OrgSmo.OrgSmo_id = PLS.OrgSmo_id
                left join dbo.v_Org as OS on OS.Org_id = OrgSmo.Org_id
                left join dbo.v_Address as UAddr on UAddr.Address_id = PS.UAddress_id
                left join dbo.KLArea as country on country.KLArea_id = UAddr.KLCountry_id
                left join dbo.KLArea as rgn on rgn.KLArea_id = UAddr.KLRgn_id
                left join dbo.KLArea as srgn on srgn.KLArea_id = UAddr.KLSubRgn_id
                left join dbo.KLArea as city on city.KLArea_id = UAddr.KLCity_id
                left join dbo.KLArea as town on town.KLArea_id = UAddr.KLSubRgn_id
                left join dbo.KLStreet as street on street.KLStreet_id = UAddr.KLStreet_id
                left join dbo.v_Address as PAddr on PAddr.Address_id = PS.PAddress_id
                left join dbo.v_KLAreaType as KLAT on KLAT.KLAreaType_id = PAddr.KLAreaType_id
                left join dbo.v_Document as D on D.Document_id = PS.Document_id
                left join dbo.v_DocumentType as DT on DT.DocumentType_id = D.DocumentType_id
                left join dbo.v_Sex as SX on SX.Sex_id = PS.Sex_id
                left join dbo.v_PayType as PT on PT.PayType_id = EPS.PayType_id
                left join dbo.v_SocStatus as SS on SS.SocStatus_id = PS.SocStatus_id
                LEFT JOIN LATERAL (
                    select
                        PrivilegeType_id,
                        PrivilegeType_Code,
                        PrivilegeType_Name
                    from dbo.v_PersonPrivilege

                    where PrivilegeType_Code in ('81', '82', '83')
                        and Person_id = PS.Person_id
                    order by PersonPrivilege_begDate desc
                    limit 1
                ) as IT ON true
                LEFT JOIN LATERAL (
                    select
                        PrivilegeType_id,
                        PrivilegeType_Code,
                        PrivilegeType_Name
                    from dbo.v_PersonPrivilege

                    where PrivilegeType_Code in ('11', '20', '91', '81', '82', '83', '84')
                        and Person_id = PS.Person_id
                    order by PersonPrivilege_begDate desc
                    limit 1
                ) as IT2 ON true
                left join dbo.v_PersonCard as PC on PC.Person_id = PS.Person_id
                    and PC.PersonCard_begDate is not null
                    and PC.PersonCard_begDate <= EPS.EvnPS_insDT
                    and (PC.PersonCard_endDate is null or PC.PersonCard_endDate > EPS.EvnPS_insDT)
                    and PC.Lpu_id = EPS.Lpu_id
                left join dbo.v_LpuSection as PHLS on PHLS.LpuSection_id = EPS.LpuSection_did
                left join dbo.v_OrgHead as OH on OH.LpuUnit_id = PHLS.LpuUnit_id and OH.OrgHeadPost_id = 13
                left join dbo.v_MedPersonal as OHMP on OHMP.Person_id = OH.Person_id
                left join dbo.v_Lpu as PreHospLpu on PreHospLpu.Lpu_id = EPS.Lpu_did
                left join dbo.v_MedStaffFact as MSF on MSF.MedStaffFact_id = EPS.MedStaffFact_pid
                left join dbo.v_OrgMilitary as PHOM on PHOM.OrgMilitary_id = EPS.OrgMilitary_did
                left join dbo.v_Org as PHO on PHO.Org_id = EPS.Org_did
                left join dbo.v_PrehospArrive as PA on PA.PrehospArrive_id = EPS.PrehospArrive_id
                left join dbo.v_Diag as DiagH on DiagH.Diag_id = EPS.Diag_did
                left join dbo.v_Diag as DiagP on DiagP.Diag_id = EPS.Diag_pid
                left join dbo.v_PrehospToxic as PHTX on PHTX.PrehospToxic_id = EPS.PrehospToxic_id
                left join dbo.v_LpuSectionTransType as LSTT on LSTT.LpuSectionTransType_id = EPS.LpuSectionTransType_id
                left join dbo.v_PrehospType as PHT on PHT.PrehospType_id = EPS.PrehospType_id
                left join dbo.v_PrehospTrauma as PHTR on PHTR.PrehospTrauma_id = EPS.PrehospTrauma_id
                left join dbo.v_MedPersonal as MPFirst on EPS.MedPersonal_pid = MPFirst.MedPersonal_id
                left join dbo.v_LpuSection as LSFirst on LSFirst.LpuSection_id = ESFirst.LpuSection_id
                left join dbo.v_LpuSectionBedProfile as LSBPFirst on LSBPFirst.LpuSectionBedProfile_id = LSFirst.LpuSectionBedProfile_id
                left join dbo.v_LeaveType as LT on LT.LeaveType_id = EPS.LeaveType_id
                left join dbo.v_EvnLeave as EL on EL.EvnLeave_pid = ESLast.EvnSection_id
                left join dbo.v_EvnDie as EDie on EDie.EvnDie_pid = ESLast.EvnSection_id
                left join dbo.v_EvnOtherLpu as EOL on EOL.EvnOtherLpu_pid = ESLast.EvnSection_id
                left join dbo.v_EvnOtherStac as EOST on EOST.EvnOtherStac_pid = ESLast.EvnSection_id
                left join dbo.v_ResultDesease as RD on RD.ResultDesease_id = COALESCE(EL.ResultDesease_id, EOL.ResultDesease_id, EOST.ResultDesease_id, EDie.ResultDesease_id)
                left join dbo.v_PersonEncrypHIV as PEH on PEH.Person_id = PS.Person_id
                LEFT JOIN LATERAL (
                    select
                         EvnStick_id
                        ,EvnStick_setDT
                        ,EvnStick_disDT
                    from
                        dbo.v_EvnStick
                    where
                        EvnStick_pid = EPS.EvnPS_id
                    order by
                        EvnStick_setDT
                    limit 1
                ) as EST ON true
                LEFT JOIN LATERAL (
                    select
                         dbo.Age2(t2.Person_Birthday, EPS.EvnPS_setDT) as Person_Age
                        ,t3.Sex_Name
                        ,t3.Sex_id
                    from
                        dbo.v_EvnStickCarePerson as t1

                        left join dbo.v_PersonState as t2 on t2.Person_id = t1.Person_id
                        left join dbo.v_Sex as t3 on t3.Sex_id = t2.Sex_id
                    where
                        t1.Evn_id = EST.EvnStick_id
                    limit 1
                ) as ESTCP ON true
                left join dbo.v_Diag as DG on DG.Diag_id = ESLast.Diag_id and COALESCE(ESLast.LeaveType_id, 0) != 5
                left join dbo.v_Diag as PAD on PAD.Diag_id = EDie.Diag_aid
                LEFT JOIN LATERAL (
                    select Diag_id
                    from dbo.v_EvnDiagPS
                    where EvnDiagPS_pid = ESLast.EvnSection_id
                        and DiagSetClass_id = 2
                    limit 1
                ) as TDGA ON true
                left join dbo.v_Diag as DGA on DGA.Diag_id = TDGA.Diag_id and COALESCE(ESLast.LeaveType_id, 0) != 5
                LEFT JOIN LATERAL (
                    select Diag_id
                    from dbo.v_EvnDiagPS
                    where EvnDiagPS_pid = ESLast.EvnSection_id
                        and DiagSetClass_id = 3
                    limit 1
                ) as TDGS ON true
                left join dbo.v_Diag as DGS on DGS.Diag_id = TDGS.Diag_id and COALESCE(ESLast.LeaveType_id, 0) != 5
                LEFT JOIN LATERAL (
                    select Diag_id
                    from dbo.v_EvnDiagPS
                    where EvnDiagPS_pid = EDie.EvnDie_id
                        and DiagSetClass_id = 2
                    limit 1
                ) as TPADA ON true
                left join dbo.v_Diag as PADA on PADA.Diag_id = TPADA.Diag_id
                LEFT JOIN LATERAL (
                    select Diag_id
                    from dbo.v_EvnDiagPS
                    where EvnDiagPS_pid = EDie.EvnDie_id
                        and DiagSetClass_id = 3
                    limit 1
                ) as TPADS ON true
                left join dbo.v_Diag as PADS on PADS.Diag_id = TPADS.Diag_id
                left join dbo.v_LpuUnitType as oLUT on oLUT.LpuUnitType_id = EOST.LpuUnitType_oid
                LEFT JOIN LATERAL (

                    select t3.YesNo_Name
                    from dbo.v_EvnUsluga as t1

                        inner join dbo.v_UslugaComplex as t2 on t2.UslugaComplex_id = t1.UslugaComplex_id
                        inner join dbo.v_YesNo as t3 on t3.YesNo_Code = 1
                    where t1.EvnUsluga_rid = EPS.EvnPS_id
                        and t2.UslugaComplex_Code = 'A12.06.011'
                        and t1.EvnUsluga_SetDT is not null
                    limit 1
                ) as IsRW ON true
                LEFT JOIN LATERAL (
                    select t3.YesNo_Name
                    from dbo.v_EvnUsluga t1
                        inner join dbo.v_UslugaComplex t2 on t2.UslugaComplex_id = t1.UslugaComplex_id
                        inner join dbo.v_YesNo as t3 on t3.YesNo_Code = 1
                    where
                        t1.EvnUsluga_rid = EPS.EvnPS_id
                        and t2.UslugaComplex_Code = 'A09.05.228'
                        and t1.EvnUsluga_setDT is not null
                    limit 1
                ) as IsAIDS ON true
            where
                EPS.EvnPS_id = '500801213437099'
            limit 1
        
