
            with Other (
                EvnSection_id
            ) as (
                select t1.EvnOtherSection_pid as EvnSection_id
                from v_EvnOtherSection t1
                where LpuSection_oid = '99560024770'

                union

                select t1.EvnOtherSectionBedProfile_pid as EvnSection_id
                from v_EvnOtherSectionBedProfile t1
                where LpuSection_oid = '99560024770'

                union

                select t1.EvnOtherStac_pid as EvnSection_id
                from v_EvnOtherStac t1
                where LpuSection_oid = '99560024770'
            ),
            curDT as (select localtimestamp as curDate)

            SELECT
                EvnSection.EvnSection_id as "EvnSection_id",
                EvnSection.EvnSection_rid as "EvnSection_rid",
                EvnSection.LpuSection_id as "LpuSection_id",
                Person.Sex_id as "Sex_id",
                case when 0=1 then PEH.PersonEncrypHIV_Encryp end as "PersonEncrypHIV_Encryp",
                case when 0=1 and PEH.PersonEncrypHIV_id is not null
                    then PEH.PersonEncrypHIV_Encryp else NULLIF(coalesce(Person.Person_SurName, '') || coalesce(' ' || Person.Person_FirName, '') || coalesce(' ' || Person.Person_SecName, ''), '')
                end as "Person_Fio",
                to_char(Person.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
                dbo.Age2(Person.Person_BirthDay, (select curDate from curDT)) as "Person_Age",
                dbo.Age_newborn(Person.Person_BirthDay, (select curDate from curDT)) as "Person_AgeMonth",
                Diag.Diag_Code as "Diag_Code",
                Diag.Diag_Name as "Diag_Name",
                coalesce(to_char(EvnSection.EvnSection_setDate, 'dd.mm.yyyy'), '') as "EvnSection_setDate",
                coalesce(to_char(EvnSection.EvnSection_disDate, 'dd.mm.yyyy'), '') as "EvnSection_disDate",
                EvnSection.Person_id as "Person_id",
                EvnSection.Server_id as "Server_id",
                EvnSection.PersonEvn_id as "PersonEvn_id",
                EvnPS.EvnPS_NumCard as "EvnPS_NumCard",
                Mes.Mes_id as "Mes_id",
                Mes.Mes_Code as "Mes_Code",
                coalesce(Mes.Mes_KoikoDni, 0) as "KoikoDni",
                EvnPS.EvnPS_id as "EvnPS_id",
                EvnSection.LpuSectionWard_id as "LpuSectionWard_id",
                EvnSection.MedPersonal_id as "MedPersonal_id",
                date_part('day', case when (EvnSection.EvnSection_disDate > (select curDate from curDT))
                        then cast('2023-07-30' as date)
                        else coalesce(EvnSection.EvnSection_disDate, cast('2023-07-30' as date)) end - EvnSection.EvnSection_setDate
                ) as "EvnSecdni",
                case when exists(
                    select *
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = EvnSection.Person_id
                    and PQ.PersonQuarantine_endDT is null
                ) then 2 else 1 end as "PersonQuarantine_IsOn"
            FROM
                v_EvnSection EvnSection
                inner join Other on Other.EvnSection_id=EvnSection.EvnSection_id
                inner join v_EvnPS EvnPS on EvnPS.EvnPS_id = EvnSection.EvnSection_pid
                
                left join lateral(
                    Select * from v_Person_all Person where Person.Server_id = EvnSection.Server_id
                    and Person.Person_id = EvnSection.Person_id
                    and Person.PersonEvn_id = EvnSection.PersonEvn_id
                    limit 1
                ) Person on true
            
                LEFT JOIN v_Diag Diag on Diag.Diag_id = EvnSection.Diag_id
                LEFT JOIN v_MesOld Mes on Mes.Mes_id = EvnSection.Mes_id
                LEFT join v_PersonEncrypHIV PEH on PEH.Person_id = Person.Person_id
                
            WHERE
                not exists (
                    select t.EvnSection_id
                    from v_EvnSection t
                    where t.EvnSection_pid = EvnSection.EvnSection_pid
                        and t.EvnSection_setDT >= EvnSection.EvnSection_disDT
                        and t.EvnSection_id <> EvnSection.EvnSection_id
                    limit 1
                )
                
            ORDER BY
                EvnSection.EvnSection_setDT desc
        
