
                
            with mv as (
                select
                localtimestamp as dt
            )
            
                
                SELECT 
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
                    PS.Person_id as "Person_id",
                    PS.Server_id as "Server_id",
                    PS.Person_Firname as "Person_Firname",
                    PS.Person_Surname as "Person_Surname",
                    PS.Person_Secname as "Person_Secname",
                    to_char(PS.Person_Birthday,  'DD.MM.YYYY') as "Person_Birthday",
                    DATEDIFF('YEAR',PS.Person_Birthday,localtimestamp)
                        +(SIGN(DATEDIFF('DAY',PS.Person_Birthday,DATEADD('YEAR',YEAR(PS.Person_Birthday)
                        -YEAR(localtimestamp),localtimestamp)))-1)/2 AS "Person_Age",
                    CAST(L.Label_id AS int) AS "Label_id",
                    (select STRING_AGG(FRG.FluoroRiskGroup_Name, ', ')
                     from v_FluoroRiskGroup FRG
                              left join v_FluoroRiskGroupPerson FRGP on FRGP.FluoroRiskGroup_id=FRG.FluoroRiskGroup_id
                     where PS.Person_id = FRGP.Person_id
                    ) as "FluoroRiskGroup_Name"
            
            
                FROM
                
         dbo.v_PersonState PS 
            left join v_PersonLabel PL on PL.Person_id = PS.Person_id and PL.Label_id=2
            left join v_Label L on L.Label_id = PL.Label_id
            left join v_FluoroPlanPerson FPP on FPP.Person_id = PL.Person_id
            left join v_FluoroRiskGroupPerson FRGP on PS.Person_id = FRGP.Person_id
                 left join v_FluoroRiskGroup FRG on FRGP.FluoroRiskGroup_id = FRG.FluoroRiskGroup_id
        
                         WHERE 
                        (1 = 1) and FPP.FluoroPlanPerson_finalDT <= DATEADD('year', -cast(2 as bigint), localtimestamp) or FPP.FluoroPlanPerson_finalDT is null AND FRG.FluoroRiskGroup_id = '12' AND PS.Person_deadDT is null and ( exists (
                        select
                            PC.personcard_id
                        from
                            
                                v_PersonCard_all PC
                                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
                            
                        where
                            PC.Person_id = PS.Person_id and PC.Lpu_id = '110' and PC.LpuAttachType_id = '1' and LR.LpuRegion_id = 905 and COALESCE(LR.LpuRegionType_id,PC.LpuRegionType_id) = '1'
                        limit 1
                    )) 
                        
                
                        ORDER BY
                            
                        PS.Person_id
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
