
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            --end variables
        
            select
                -- select
        
                distinct
            
            EPL.EvnPL_id as "EPL_ID",
            
            EvnUsluga.EvnUsluga_pid as "EPZ_ID",
            EvnUsluga.EvnUsluga_id as "EUS_ID",
            EvnUsluga.EvnClass_SysNick as "EU_CLASS",
            RTRIM(coalesce(to_char(EvnUsluga.EvnUsluga_setDT, 'DD.MM.YYYY'), '')) as "SETDATE",
            EvnUsluga.EvnUsluga_setTime as "SETTIME",
            dbfusluga.UslugaComplex_Code as "USL_CODE",
            EvnUsluga.EvnUsluga_Kolvo as "KOLVO",
            dbfup.UslugaPlace_Code as "UP_CODE",
            dbfmp.MedPersonal_TabCode as "MP_CODE",
            dbfpt.PayType_Code as "PT_CODE"
        
            -- end select
            from
                -- from
        
                    v_EvnPL EPL
                        inner join v_EvnVizitPL  on v_EvnVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  v_EvnVizitPL.Lpu_id = '2834'
                        inner join v_PersonState PS on  EPL.Person_id = PS.Person_id
                        inner join Evn on Evn.Evn_id = EPL.EvnPL_id and Evn.EvnClass_id = 3 and Evn.Evn_deleted = 1 and Evn.Lpu_id = '2834'
                        left join v_Polis pls on pls.Polis_id = ps.Polis_id
                        -- left join NationalityStatus ns on ns.NationalityStatus_id = ps.NationalityStatus_id
                        left join lateral(
                            select
                                EvnPLBase.EvnPLBase_IsFinish,
                                EvnPLBase.EvnPLBase_VizitCount
                            from v_EvnPLBase EvnPLBase
                            where EvnPLBase.EvnPLBase_id = Evn.Evn_id
                            limit 1
                             ) as EvnPLBase on true
                
                    left join lateral(
                        select
                            EVPL.EvnVizitPL_id,
                            EVPL.HealthKind_id,
                            EVPL.VizitType_id,
                            EVPL.MedPersonal_id,
                            EVPL.Lpu_id,
                            EVPL.Diag_id,
                            EVPL.LpuSection_id,
                            EVPL.DeseaseType_id,
                            EVPL.MedPersonal_sid,
                            EVPL.EvnVizitPL_isPaid,
                            EVPL.ServiceType_id,
                            EVPL.TreatmentClass_id,
                            EVPL.EvnVizitPL_setDT,
                            EVPL.UslugaComplex_id,
                            EVPL.EvnVizitPL_IsInReg,
                            EVPL.VizitClass_id
                        from v_EvnVizitPL EVPL
                        where EVPL.EvnVizitPL_pid = EPL.EvnPL_id and
                              EVPL.EvnVizitPL_Index = coalesce(EVPL.EvnVizitPL_IndexMinusOne, 0)
                        limit 1
                    ) as EVPL on true
                
                        inner join v_EvnUsluga EvnUsluga on EvnUsluga.EvnUsluga_rid = EPL.EvnPL_id and EvnUsluga.Lpu_id = '2834'
                        left join v_PayType as dbfpt on dbfpt.PayType_id = EvnUsluga.PayType_id
                        left join v_UslugaComplex as dbfusluga on dbfusluga.UslugaComplex_id = EvnUsluga.UslugaComplex_id
                        left join v_UslugaPlace as dbfup on dbfup.UslugaPlace_id = EvnUsluga.UslugaPlace_id
                        left join v_MedPersonal dbfmp on dbfmp.MedPersonal_id = EvnUsluga.MedPersonal_id and dbfmp.Lpu_id = Evn.Lpu_id
                     inner join v_PersonState PS3 on Evn.Person_id = PS3.Person_id 
            inner join v_LpuSection LS on LS.LpuSection_id = EPL.LpuSection_id
            inner join LpuUnit LU on LU.LpuUnit_id = LS.LpuUnit_id
            left join lateral(
                select ServiceEvnStatus_id
                from v_ServiceEvnHist
                where Evn_id = EPL.EvnPL_id and ServiceEvnList_id = 1
                order by ServiceEvnHist_id desc
                limit 1
            ) as SEH1 on true
            left join v_ServiceEvnStatus SES1 on SES1.ServiceEvnStatus_id = SEH1.ServiceEvnStatus_id
        
                        -- end from
                    where
                        -- where
                        (1 = 1) and EPL.EvnPL_setDate::date >= '2022-01-01' and EPL.EvnPL_setDate::date <= '2022-02-28' and EPL.EvnPL_disDate::date >= '2022-01-01' and EPL.EvnPL_disDate::date <= '2022-02-28' and EVPL.TreatmentClass_id = '4'
                        -- end where
                
