
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count( distinct EPLS.EvnPLStom_id  ) AS cnt 
            from
                -- from
         dbo.v_PersonState PS inner join v_EvnPLStom EPLS on EPLS.Person_id = PS.Person_id
                        left join v_Diag as dbfdiag on dbfdiag.Diag_id = EPLS.Diag_id
                        left join v_Lpu as dbflpu on dbflpu.Lpu_id = EPLS.Lpu_id
                        left join ResultClass dbfrc on dbfrc.ResultClass_id = EPLS.ResultClass_id
                        left join DeseaseType dbfdt on dbfdt.DeseaseType_id = EPLS.DeseaseType_id
                        left join PrehospDirect dbfpd on dbfpd.PrehospDirect_id = EPLS.PrehospDirect_id
                        left join v_Lpu dbfprehosplpu on dbfprehosplpu.Lpu_id = EPLS.Lpu_did
                        left join LpuSection dbflsd on dbflsd.LpuSection_id = EPLS.LpuSection_did
                        left join YesNo dbfift on dbfift.YesNo_id = EPLS.EvnPLStom_IsFirstTime
                        left join DirectClass dbfdc on dbfdc.DirectClass_id = EPLS.DirectClass_id
                        left join v_Lpu dbflpudir on dbflpudir.Lpu_id = EPLS.Lpu_oid
                        left join LpuSection dbflsdir on dbflsdir.LpuSection_id = EPLS.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 = EPLS.Person_id
                            order by PersonChild.PersonChild_insDT desc
                            limit 1
                        ) dbfinv on true
                    
                                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 Sex PrsSex on PrsSex.Sex_id = PS.Sex_id
                    left join SocStatus PrsSoc on PrsSoc.SocStatus_id = PS.SocStatus_id
                    left join PersonChild PrsPCh on PrsPCh.Person_id = PS.Person_id
                    left join YesNo PrsIsInv on PrsIsInv.YesNo_id = PrsPCh.PersonChild_IsInvalid
                    left join Diag PrsInvD on PrsInvD.Diag_id = PrsPCh.Diag_id
                    left join Polis PrsPol on PrsPol.Polis_id = PS.Polis_id
                    left join OMSSprTerr PrsOMSST on PrsOMSST.OMSSprTerr_id = PrsPol.OMSSprTerr_id
                    left join PolisType PrsPolTp on PrsPolTp.PolisType_id = PrsPol.PolisType_id
                    left join v_OrgSmo PrsOS on PrsOS.OrgSmo_id = PrsPol.OrgSmo_id
                    left join v_Org PrsOSO on PrsOSO.Org_id = PrsOS.Org_id
                    left join v_Address_all PrsUA on PrsUA.Address_id = PS.UAddress_id
                    left join v_Address_all PrsPA on PrsPA.Address_id = PS.PAddress_id
                    left join Document PrsDoc on PrsDoc.Document_id = PS.Document_id
                    left join DocumentType PrsDocTp on PrsDocTp.DocumentType_id = PrsDoc.DocumentType_id
                    left join v_OrgDep PrsOrgD on PrsOrgD.OrgDep_id = PrsDoc.OrgDep_id
                
            left join lateral(
                select *
                from v_MedPersonal
                where MedPersonal_id = EPLS.MedPersonal_id
                  and Lpu_id = '13101851'
                order by case when Lpu_id = '13101851' then 1 else 2 end
                limit 1
            ) as MP on true
        
                        -- end from
                    where
                        -- where
                        (1 = 1) and coalesce(dbfdiag.Diag_Code, '') not in('B18.0','B18.1','B17.1','B18.2','Z11.3','Z03.2','Z72.2') and coalesce(dbfdiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(dbfdiag.Diag_Code, '') not between 'A50.0' and 'A64' and coalesce(dbfdiag.Diag_Code, '') not between 'B16.0' and 'B16.9' and coalesce(dbfdiag.Diag_Code, '') not between 'A15.0' and 'A19.9' and coalesce(dbfdiag.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(dbfdiag.Diag_Code, '') not between 'F20.0' and 'F99' and coalesce(dbfdiag.Diag_Code, '') not between 'Z50.2' and 'Z50.3' and coalesce(dbfdiag.Diag_Code, '') not between 'F10.0' and 'F19.9'
                        -- end where
                
                    
                
