
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count( distinct EPL.EvnPL_id  ) AS cnt 
            from
                -- from
        
            v_EvnPL EPL
            
                    inner join v_EvnVizitPL EVizitPL on EVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and EVizitPL.Lpu_id = '150006'
                    INNER JOIN v_PersonState PS ON PS.Person_id = EPL.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 v_Polis pls on pls.Polis_id = ps.Polis_id
                
                        left join v_Diag as dbfdiag on dbfdiag.Diag_id = EPL.Diag_id
                        left join v_Lpu as dbflpu on dbflpu.Lpu_id = EPL.Lpu_id
                        left join ResultClass dbfrc on dbfrc.ResultClass_id = EPL.ResultClass_id
                        left join DeseaseType dbfdt on dbfdt.DeseaseType_id = EPL.DeseaseType_id
                        left join PrehospDirect dbfpd on dbfpd.PrehospDirect_id = EPL.PrehospDirect_id
                        left join v_Lpu dbfprehosplpu on dbfprehosplpu.Lpu_id = EPL.Lpu_did
                        left join LpuSection dbflsd on dbflsd.LpuSection_id = EPL.LpuSection_did
                        left join DirectClass dbfdc on dbfdc.DirectClass_id = EPL.DirectClass_id
                        left join v_Lpu dbflpudir on dbflpudir.Lpu_id = EPL.Lpu_oid
                        left join LpuSection dbflsdir on dbflsdir.LpuSection_id = EPL.LpuSection_oid
                        left join lateral(
                            select
                                CASE WHEN PersonChild.PersonChild_IsInvalid = 2 THEN '1' ELSE '0' END as PersonChild_IsInvalid_Code,
                                PersonSprTerrDop.PersonSprTerrDop_Code as PermRegion_Code
                            from
                                PersonChild
                                left join PersonSprTerrDop on PersonSprTerrDop.PersonSprTerrDop_id = PersonChild.PersonSprTerrDop_id
                            where PersonChild.Person_id = EPL.Person_id
                            order by PersonChild.PersonChild_insDT desc
                            limit 1
                        ) as dbfinv on true
                    
                            left join v_Polis pls2 on pls2.Polis_id = PS.Polis_id
                            left join SocStatus as dbfss on dbfss.SocStatus_id = PS.SocStatus_id
                            left join Address dbfaddr on dbfaddr.Address_id = coalesce(PS.PAddress_id, PS.UAddress_id)
                            left join KLStreet dbfkls on dbfkls.KLStreet_id = dbfaddr.KLStreet_id
                            left join KLArea dbfkla on dbfkla.KLArea_id = COALESCE(dbfaddr.KLTown_id, dbfaddr.KLCity_id, dbfaddr.KLSubRgn_id, dbfaddr.KLRgn_id) and dbfkls.KLStreet_id is null
                        
            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.EvnClass_id = 3 and EPL.Lpu_id = '150006' and EPL.EvnPL_setDt >= '2020-08-06'::timestamp and EPL.EvnPL_setDt < '2020-12-31'::timestamp + interval '1 day' and EPL.EvnPL_disDt >= '2020-08-06'::timestamp and EPL.EvnPL_disDt < '2020-12-31'::timestamp + interval '1 day' and coalesce(dbfdiag.Diag_Code, '') not in('B18.1','B18.2','Z21','Z20.2','B18.0','B17.1','R75') and coalesce(dbfdiag.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(dbfdiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(dbfdiag.Diag_Code, '') not between 'B16.0' and 'B16.9' and coalesce(dbfdiag.Diag_Code, '') not between 'A50.0' and 'A64' and coalesce(dbfdiag.Diag_Code, '') not between 'F10.0' and 'F19.9' and coalesce(dbfdiag.Diag_Code, '') not between 'F20.0' and 'F99'
                        -- end where
                
                    
                
