
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count( distinct PS.Person_id  ) AS cnt 
            from
                -- from
         dbo.v_PersonState PS 
            inner join (
                select PR.*
                from
                    (
                        select
                            PR.Person_id,
                            PR.PersonRegister_id,
                            ROW_NUMBER() OVER (PARTITION BY Person_id ORDER BY PR.PersonRegister_setDate, PR.PersonRegister_id) r
                        from dbo.v_PersonRegister PR
                        where
                            PR.MorbusType_id in (88, 84, 89, 50, 110, 111, 112, 113)
                            
                    ) PersonRegister
                    inner join dbo.v_PersonRegister PR on PR.PersonRegister_id = PersonRegister.PersonRegister_id
                where PersonRegister.r = 1
            ) PR on PR.Person_id = PS.Person_id
            left join dbo.v_PersonCard PC on PC.Person_id = PS.Person_id
         and PC.LpuAttachType_id = 1 
            left join dbo.v_Lpu Lpu on Lpu.Lpu_id = PC.Lpu_id
            left join dbo.v_LpuRegion LR on LR.LpuRegion_id = PC.LpuRegion_id
            left join lateral (
                select *
                from dbo.BSKRegistry r
                where (1=1)
                    and r.Person_id = PS.Person_id
                    and r.MorbusType_id in (88,84,89,50,110,111,112,113, 143)
                    
                order by r.BSKRegistry_setDate desc
                limit 1
            ) as R on true
            left join dbo.v_PersonRegisterOutCause PROUT on PROUT.PersonRegisterOutCause_id = PR.PersonRegisterOutCause_id
            left join dbo.v_Diag DP on PR.Diag_id = DP.Diag_id
            left join dbo.v_pmUserCache MP on MP.PMUser_id = R.pmUser_insID
            left join v_RiskType rt on rt.RiskType_id = PR.RiskType_id and PR.MorbusType_id is null
        
                        -- end from
                    where
                        -- where
                        (1 = 1)
                        -- end where
                
                    
