
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            --end variables
        
            select
                 count( distinct EVizitPL.EvnVizitPL_id  ) AS cnt 
            from
                -- from
        
                    v_EvnVizitPL EVizitPL
                     inner join v_PersonState PS on EVizitPL.Person_id = PS.Person_id 
                    inner join v_EvnPL EPL on EVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  EVizitPL.Lpu_id = '39'
                    inner join Evn on Evn.Evn_id = EVizitPL.EvnVizitPL_pid and Evn.EvnClass_id = 3 and Evn.Evn_deleted = 1 and Evn.Lpu_id = '39'
                    --	inner join v_PersonState PS on  EPL.Person_id = PS.Person_id
                    --	left join v_Polis pls on pls.Polis_id = ps.Polis_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*/
                    
                inner join lateral(
                    select
                        EVPL.EvnVizitPL_id,
                        EVPL.EvnVizitPL_pid
                    from
                        v_EvnVizitPL EVPL
                        
                    where EVPL.Lpu_id = '39'
                     and EvnVizitPL_id = EVizitPL.EvnVizitPL_id limit 1 
                    ) as EVPL2 on true
            
                
                    --inner join v_MedPersonal as evplmp on evplmp.MedPersonal_id = EVizitPL.MedPersonal_id and evplmp.Lpu_id = Evn.Lpu_id
                    inner join v_MedPersonal as evplmp on evplmp.MedPersonal_id = EVizitPL.MedPersonal_id and evplmp.Lpu_id = EVizitPL.Lpu_id
                    left join v_Diag as evpldiag on evpldiag.Diag_id = EVizitPL.Diag_id
                    left join v_LpuSection as evplls on evplls.LpuSection_id = EVizitPL.LpuSection_id
                    left join v_PayType as evplpt on evplpt.PayType_id = EVizitPL.PayType_id
                    left join v_VizitType as evplvt on evplvt.VizitType_id = EVizitPL.VizitType_id
                    left join v_ServiceType as evplst on evplst.ServiceType_id = EVizitPL.ServiceType_id
                    left join v_HealthKind as evplhk on evplhk.HealthKind_id = EVizitPL.HealthKind_id
                    left join v_VizitType VT on VT.VizitType_id = EVizitPL.VizitType_id
                 left join v_Polis pls on pls.Polis_id = ps.Polis_id  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 coalesce(evpldiag.Diag_Code, '') not in('F99.1','F99.1','R76.2','R75','Z21','O98.7','Z20.6','Z71.7') and coalesce(evpldiag.Diag_Code, '') not between 'A50.0' and 'A66.1' and coalesce(evpldiag.Diag_Code, '') not between 'B85.0' and 'B85.4' and coalesce(evpldiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(evpldiag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(evpldiag.Diag_Code, '') not between 'F00.0' and 'F44.9' and coalesce(evpldiag.Diag_Code, '') not between 'F48.0' and 'F99'
                        -- end where
                
                        
                    
