
                
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            
                
                SELECT 
        
            case when EUP.Lpu_id = '13101856' and coalesce(EUP.MedPersonal_id, '890101000001495') = '890101000001495' then 'edit' else 'view' end as "accessType",
            doc.EvnXml_id as "EvnXml_id",
            
            EUP.EvnUslugaPar_id as "EvnUslugaPar_id",
            EvnUslugaPar_pid as "EvnUslugaPar_pid",
            EUP.Person_id as "Person_id",
            EUP.PersonEvn_id as "PersonEvn_id",
            EUP.Server_id as "Server_id",
            coalesce( to_char(EUP.EvnUslugaPar_disDate, 'DD.MM.YYYY'), to_char(EUP.EvnUslugaPar_setDate, 'DD.MM.YYYY')) as "EvnUslugaPar_setDate",
            coalesce(EUP.EvnUslugaPar_IsSigned, 1) as "EvnUslugaPar_IsSigned",
            RTRIM(LS.LpuSection_Name) as "LpuSection_Name",
            RTRIM(MP.Person_Fio) as "MedPersonal_Fio",
            lpu.Lpu_Name as "Lpu_Name",
            CASE WHEN EUP.UslugaComplex_id IS NULL THEN RTRIM(Usluga.Usluga_Code) ELSE RTRIM(UslugaComplex.UslugaComplex_Code) END as "Usluga_Code",
            CASE WHEN EUP.UslugaComplex_id IS NULL THEN RTRIM(Usluga.Usluga_Name) ELSE RTRIM(UslugaComplex.UslugaComplex_Name) END as "Usluga_Name",
            RTRIM(PT.PayType_Name) as "PayType_Name",
            coalesce(EUP.EvnUslugaPar_Kolvo, 0) as "EvnUslugaPar_Kolvo",
            coalesce(PostMed.PostMed_Name, '') as "PostMed_Name",
            to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
            case when ecp.EvnCostPrint_IsNoPrint = 2 then 'Отказ от справки' when ecp.EvnCostPrint_IsNoPrint = 1 then 'Справка выдана' else '' end as "EvnCostPrint_IsNoPrintText",
            coalesce(O2.Org_Nick, L.Lpu_Nick, Org.Org_Nick, L2.Lpu_Nick,  L3.Lpu_Nick) as "Referral_Org_Nick", -- направившая МО
            (select LpuSection_Name from v_LpuSection where LpuSection_id = EUP.LpuSection_did) as "LSD"
        
                ,RTRIM(PS.Person_SurName) as "Person_Surname"
                ,RTRIM(PS.Person_FirName) as "Person_Firname"
                ,RTRIM(PS.Person_SecName) as "Person_Secname"
                ,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
                ,dbo.Age2(PS.Person_BirthDay, (select dt from mv))  as "Person_Age"
                ,to_char(PS.Person_deadDT, 'DD.MM.YYYY') as "Person_deadDT"
            
            
                FROM
                
         dbo.v_PersonState PS  inner join v_EvnUslugaPar EUP on EUP.Person_id = PS.Person_id and (
                EUP.Lpu_id = '13101856'
                OR EUP.Lpu_uid = '13101856'
            )
            left join v_EvnCostPrint ecp on ecp.Evn_id = EUP.EvnUslugaPar_id
            left join v_LpuSection LS on LS.LpuSection_id = EUP.LpuSection_uid
            left join v_Lpu lpu on lpu.Lpu_id = LS.Lpu_id
            left join lateral(
                select Person_Fio
                from v_MedPersonal
                where MedPersonal_id = EUP.MedPersonal_id
                limit 1
            ) MP on true
            left join Usluga on Usluga.Usluga_id = EUP.Usluga_id
            left join UslugaComplex on UslugaComplex.UslugaComplex_id = EUP.UslugaComplex_id
            left join v_Evn EvnParent on EvnParent.Evn_id = EUP.EvnUslugaPar_pid
            left join v_PayType PT on PT.PayType_id = EUP.PayType_id
            left join v_MedStaffFact MedStaffFact on MedStaffFact.MedStaffFact_id = EUP.MedStaffFact_id
            left join v_PostMed PostMed on PostMed.PostMed_id = MedStaffFact.Post_id
            left join v_EvnDirection_all ED on EUP.EvnDirection_id = ED.EvnDirection_id
            left join v_Lpu LD on LD.Lpu_id = ED.Lpu_sid
            left join Org on Org.Org_id = ED.Org_sid
            left join v_Lpu L ON L.Lpu_id = ED.Lpu_sid -- NGS
            left join v_Lpu L2 ON L2.Lpu_id = EUP.Lpu_did -- NGS
            left join v_Lpu L3 on L3.Org_id = EUP.Org_uid
            left join Org O2 on O2.Org_id = EUP.Org_did
        
                left join lateral(
                    select EvnXml.EvnXml_id
                    from v_EvnXml  EvnXml
                    where Evn_id = EUP.EvnUslugaPar_id
                    order by EvnXml_insDT desc
                    limit 1
                ) doc on true
            
                         WHERE 
                        (1 = 1) and eup.EvnUslugaPar_setDate is not null
                and exists(
                    select 1
                    from v_Person_all PStmp
                    where PStmp.Person_id = PS.Person_id
             and PStmp.Person_BirthDay = cast('2022-02-17' as timestamp))  and (EvnParent.EvnClass_SysNick <> 'EvnUslugaPar' or EvnParent.EvnClass_SysNick is null)
                        
                
                        ORDER BY
                            
                        EUP.EvnUslugaPar_id
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
