
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            ),
            --end variables
        
                -- addit with
                
                        PCSD1 as (
                            select
                                max(Personcard_id) as Personcard_id,
                                Person_id
                            from
                                v_PersonCard_all pc
                            where
                                pc.Lpu_id = '2222030'
                            group by Person_id
                        )
                -- end addit with
            
            select
                 count(  * ) AS cnt 
            from
                -- from
        
                	    PCSD1
                	    inner join v_PersonState_all PS on PCSD1.person_id=PS.person_id
                     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 = '2222030' 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 (PC.PersonCard_endDate is null or PC.PersonCard_endDate::timestamp > (select dt from mv)) and PC.Lpu_id = '2222030' and PC.Lpu_id = '2222030' and PC.LpuAttachType_id = '1' and PC.PersonCard_begDate >= '2003-03-04'::timestamp and PC.PersonCard_begDate <= '2022-03-04'::timestamp and LR.LpuRegion_id = 530 and COALESCE(LR.LpuRegionType_id,PC.LpuRegionType_id) = '2'
                        -- end where
                
                    
                
