
                
            with mv as (
                select
                dbo.tzGetDate() as dt
            ),
            
                
                
                        PCSD1 as (
                            select
                                max(Personcard_id) as Personcard_id,
                                Person_id
                            from
                                v_PersonCard_all pc
                            where
                                pc.Lpu_id = '37'
                            group by Person_id
                        )
                
                SELECT 
        
            DOr.PersonDispOrp_id as "PersonDispOrp_id",
            DOr.AgeGroupDisp_id as "AgeGroupDisp_id",
            DOr.Org_id as "Org_id",
            PS.Person_id as "Person_id",
            PS.Server_id as "Server_id",
            PS.PersonEvn_id as "PersonEvn_id",
            Sex.Sex_Name as "Sex_Name",
            AGD.AgeGroupDisp_Name as "AgeGroupDisp_Name",
            to_char(DOr.PersonDispOrp_begDate, 'DD.MM.YYYY') as "PersonDispOrp_begDate",
            EPLDTI.EvnPLDispTeenInspection_id as "EvnPLDispTeenInspection_id",
            case when DOr.Org_id is null then 'false' else 'true' end as "OrgExist",
            case when EPLDTI.EvnPLDispTeenInspection_id is null then 'false' else 'true' end as "ExistsDOPL"
        
                ,rtrim(PS.Person_SurName) as "Person_Surname"
                ,rtrim(PS.Person_FirName) as "Person_Firname"
                ,rtrim(PS.Person_SecName) as "Person_Secname"
                ,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
            
            
                FROM
                
        
                	    PCSD1
                        inner join lateral (
                            select *
                            from v_PersonState_all PS
                            where PCSD1.person_id = PS.person_id
                            limit 1
                        ) PS on true
                    
            inner join v_PersonDispOrp DOr on DOr.Person_id = PS.Person_id and DOr.Lpu_id = '37'
            left join Sex on PS.Sex_id = Sex.Sex_id
            left join v_EvnPLDispTeenInspection EPLDTI on EPLDTI.PersonDispOrp_id = DOr.PersonDispOrp_id
            left join v_AgeGroupDisp AGD on AGD.AgeGroupDisp_id = DOr.AgeGroupDisp_id
        
                         WHERE 
                        (1 = 1) and DOr.PersonDispOrp_Year = 2022  and DOr.CategoryChildType_id IN (10) and lower(PS.Person_FirName) like lower('ли')||'%' and lower(PS.Person_SecName) like lower('ег')||'%' and lower(PS.Person_SurName) like lower('са')||'%' and date_part('year', PS.Person_BirthDay) = 2022
                        
                
                        ORDER BY
                            
                        
                        PS.Person_SurName,
                        PS.Person_FirName,
                        PS.Person_SecName
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
