
            with UslugaPid (
                EvnUsluga_id
            ) as (
                select EvnUsluga_id from v_EvnUsluga EU
                where  (1=1)
                and EU.Person_id = 7068281
                and (EU.EvnClass_SysNick in ('EvnUslugaCommon', 'EvnUslugaOper'))
                and coalesce(EU.EvnUsluga_IsVizitCode, 1) = 1
            )
            select
                EU.EvnUsluga_id as "EvnUsluga_id",
                EPS.EvnPS_id as "EvnPS_id",
                EU.EvnUsluga_pid as "EvnUsluga_pid",
                to_char(EU.EvnUsluga_setDate, 'dd.mm.yyyy') as "EvnUsluga_setDate",
                EU.EvnUsluga_setTime as "EvnUsluga_setTime",
                UC.UslugaComplex_Code as "Usluga_Code",
                coalesce(Usluga.Usluga_Name, UC.UslugaComplex_Name) as "Usluga_Name",
                l.Lpu_Nick as "Lpu_Nick"
            from
                v_EvnUsluga EU
                left join v_Evn EvnParent on EvnParent.Evn_id = EU.EvnUsluga_pid
                left join v_Usluga Usluga on Usluga.Usluga_id = EU.Usluga_id
                left join v_UslugaComplex UC on UC.UslugaComplex_id = EU.UslugaComplex_id
                left join v_EvnSection ES on ES.EvnSection_id = EU.EvnUsluga_pid
                left join v_EvnPS EPS on EPS.EvnPS_id = ES.EvnSection_pid
                left join v_Lpu l on EU.Lpu_id = l.Lpu_id
            where
                exists(
                    Select
                        1
                    from
                        UslugaPid
                    where UslugaPid.EvnUsluga_id = EU.EvnUsluga_id
                    limit 1
                )

                and coalesce(EU.EvnUsluga_IsVizitCode, 1) = 1
                and EPS.EvnPS_id is not null
                and UC.UslugaComplex_Code in (select UslugaComplex_Code from dbo.BSKRegistryUslugaComplex where BSKRegistryUslugaComplex_EventType = 1)
            order by EU.EvnUsluga_setDate desc
        
