
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            --end variables
        
            select
                 count(  * ) AS cnt 
            from
                -- from
        v_PersonState_all PS  inner join lateral
                        (select * from  v_PersonCard PC where PC.Person_id = PS.Person_id) PC on true
                left join lateral (
                    select
                        PAC.personambulatcard_id
                    from
                        v_PersonAmbulatCard PAC
                    where
                        PAC.Person_id = pc.Person_id
                        and PAC.personambulatcard_num = pc.PersonCard_code
                    limit 1
                ) PAC on true
                left join lateral(
                    select ACLT.AmbulatCardLocatType_Name
                    from
                        v_PersonAmbulatCardLocat PACL
                        left join AmbulatCardLocatType ACLT on ACLT.AmbulatCardLocatType_id = PACL.AmbulatCardLocatType_id
                        left join v_PersonAmbulatCardLink PACLink on PACLink.PersonAmbulatCard_id = PACL.PersonAmbulatCard_id
                    where pc.PersonCard_id = PACLink.PersonCard_id
                    order by PACL.PersonAmbulatCardLocat_begDate desc
                    limit 1
                ) as PACLT on true
                left join v_LpuRegion LR on LR.LpuRegion_id = PC.LpuRegion_id
                left join v_LpuRegion LR_Fap on LR_Fap.LpuRegion_id = PC.LpuRegion_fapid
                left join v_Lpu AttachLpu on AttachLpu.Lpu_id = PC.Lpu_id
                left join Address uaddr on uaddr.Address_id = ps.UAddress_id
                left join Address paddr on paddr.Address_id = ps.PAddress_id
                left join v_Polis pls on pls.Polis_id = ps.Polis_id
                left join PersonRefuse PRef on PRef.Person_id = ps.Person_id and PRef.PersonRefuse_Year = date_part('year', (select dt from mv)) + 1
                left join lateral(
                    select max(case when Lpu_id = '2852' then 1 else 0 end) as OwnLpu
                    from PersonDisp
                    where Person_id = ps.Person_id
                      and (PersonDisp_endDate is null or PersonDisp_endDate > (select dt from mv))
                      and Sickness_id in (1, 3, 4, 5, 6, 7, 8)
                ) as disp on true
            
                        -- end from
                    where
                        -- where
                        (1 = 1) and coalesce(PS.Person_IsUnknown,1) != 2  and exists (select AR.Address_id from Address AR  where AR.Address_id = PS.UAddress_id and AR.KLCountry_id = '643' and AR.KLRgn_id = '52' and AR.KLSubRgn_id = '1230' and AR.KLCity_id = '3226' and AR.KLStreet_id = '397021')  and (PC.PersonCard_endDate is null or PC.PersonCard_endDate::timestamp > (select dt from mv)) and PC.Lpu_id = '2852' and PC.Lpu_id = '2852' and PC.LpuAttachType_id = '1' and COALESCE(LR.LpuRegionType_id,PC.LpuRegionType_id) = '1'
                        -- end where
                
                    
                
