
                
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            
                
                SELECT 
        
                    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,GETDATE())
                        +(SIGN(DATEDIFF('DAY',PS.Person_Birthday,DATEADD('YEAR',YEAR(PS.Person_Birthday)
                        -YEAR(GETDATE()),GETDATE())))-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
            
        
                         WHERE 
                        (1 = 1) and FPP.FluoroPlanPerson_finalDT <= DATEADD('year', -cast(1 as bigint), dbo.tzGetDate()) or FPP.FluoroPlanPerson_finalDT is null AND PS.Person_deadDT is null and lower(PS.Person_FirName) like lower('александр')||'%' and lower(PS.Person_SecName) like lower('валерьевич')||'%' and lower(PS.Person_SurName) like lower('петрухин')||'%'
                        
                
                        ORDER BY
                            
                        PS.Person_id
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
