
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count( distinct EVPLS.EvnVizitPLStom_id  ) AS cnt 
            from
                -- from
         dbo.v_PersonState PS 
                    inner join v_EvnVizitPLStom as EVPLS on EVPLS.Person_id = PS.Person_id and EVPLS.Lpu_id = '891'
                    inner join v_EvnPLStom EPLS on EPLS.EvnPLStom_id = EVPLS.EvnVizitPLStom_pid and EPLS.Lpu_id = '891' and EPLS.EvnClass_id = 6
                
                    left join v_LpuSection as evplls on evplls.LpuSection_id=EVPLS.LpuSection_id
                    left join v_PayType as evplpt on evplpt.PayType_id=EVPLS.PayType_id
                    left join v_VizitType as evplvt on evplvt.VizitType_id=EVPLS.VizitType_id
                    left join v_ServiceType as evplst on evplst.ServiceType_id=EVPLS.ServiceType_id
                    left join v_Diag as evpldiag on evpldiag.Diag_id = EVPLS.Diag_id
                 left join v_Polis pls on pls.Polis_id = ps.Polis_id
                 left join v_EvnCostPrint ecp on ecp.Evn_id = EPLS.EvnPLStom_id 
            left join lateral(
                select *
                from v_MedPersonal
                where MedPersonal_id = EVPLS.MedPersonal_id
                  and Lpu_id = '891'
                order by case when Lpu_id = '891' then 1 else 2 end
                limit 1
            ) as MP on true
        
                        -- end from
                    where
                        -- where
                        (1 = 1)
                and exists (
                    select 1
                    from v_EvnVizitPLStom EVPLS2
                    where (1 = 1) and EVPLS2.Lpu_id = '891'
             and EVPLS2.EvnVizitPLStom_id = EVPLS.EvnVizitPLStom_id and EVPLS2.LpuSection_id = '50101000002575' and EVPLS2.MedStaffFact_id = '50101000058373' and EVPLS2.MedPersonal_id = '50101000017274') and coalesce(evpldiag.Diag_Code, '') not in('A53.0','A59.0','A54.0','R75','Z21','Z11.4','Z20.6','Z21','Z71.7','Z83.0','R75','F02.8','F02.4') and coalesce(evpldiag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(evpldiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(evpldiag.Diag_Code, '') not between 'F00.0' and 'F02.3' and coalesce(evpldiag.Diag_Code, '') not between 'F03' and 'F99'
                        -- end where
                
                    
                
