
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            --end variables
        
            select
                 count( distinct EPL.EvnPL_id  ) AS cnt 
            from
                -- from
        
                    v_EvnPL EPL
                        inner join v_EvnVizitPL  on v_EvnVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  v_EvnVizitPL.Lpu_id = '2855'
                        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 = '2855'
                        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
                        left join v_Diag EVPLD on EVPLD.Diag_id = EPL.Diag_id
                        left join v_YesNo IsFinish on IsFinish.YesNo_id = EPL.EvnPL_IsFinish
                        left join v_HealthKind as HK on HK.HealthKind_id = EVPL.HealthKind_id
                        left join v_VizitType VT on VT.VizitType_id = EVPL.VizitType_id
                        left join Lpu dlpu on dlpu.Lpu_id = EPL.Lpu_did
                        left join lateral(
                            select MP.Person_Fio, MP.MedPersonal_id
                            from v_MedPersonal MP
                            where MP.MedPersonal_id = EPL.MedPersonal_id and MP.Lpu_id = EPL.Lpu_id
                            limit 1 ) as MP on true
                        
                inner join lateral(
                    select
                        EVPL.EvnVizitPL_id,
                        EVPL.EvnVizitPL_pid
                    from
                        v_EvnVizitPL EVPL
                        
                    where EVPL.Lpu_id = '2855'
                     and EvnVizitPL_pid = EPL.EvnPL_id limit 1 
                    ) as EVPL2 on true
            
                 LEFT JOIN LATERAL (
                SELECT
                    EvnCostPrint_setDT,
                    EvnCostPrint_IsNoPrint
                FROM v_EvnCostPrint ecp
                where ecp.Evn_id = EPL.EvnPL_id
                LIMIT 1
            ) ecp on true
            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 MP.MedPersonal_id is not NULL  and EPL.Lpu_id = '2855'  and coalesce(EVPLD.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(EVPLD.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(EVPLD.Diag_Code, '') not between 'F20.0' and 'F99' and coalesce(EVPLD.Diag_Code, '') not between 'F10.0' and 'F19.9' and coalesce(EVPLD.Diag_Code, '') not between 'A50.0' and 'A64' and Evn.pmUser_insID = '394298903991' and Evn.Evn_insDT::date >= '2022-03-15'::date and Evn.Evn_insDT::date <= '2022-03-16'::date
                        -- end where
                
                    
                
