
                
            with mv as (
                select
                localtimestamp as dt
            )
            
                
                SELECT 
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
            EPLDTI.EvnPLDispTeenInspection_id as "EvnPLDispTeenInspection_id",
            EPLDTI.lpu_id as "Lpu_id",
            EPLDTI.EvnPLDispTeenInspection_fid as "EvnPLDispTeenInspection_fid",
            HGT.HealthGroupType_Name as "HealthGroupType_Name",
            coalesce(EPLDTI.EvnPLDispTeenInspection_IsTransit, 1) as "EvnPLDispTeenInspection_IsTransit",
            EPLDTI.Person_id as "Person_id",
            EPLDTI.Server_id as "Server_id",
            EPLDTI.PersonEvn_id as "PersonEvn_id",
            coalesce(UAdd.Address_Nick, UAdd.Address_Address) as "ua_name",
            coalesce(PAdd.Address_Nick, PAdd.Address_Address) as "pa_name",
            Sex.Sex_Name as "Sex_Name",
            AGD.AgeGroupDisp_Name as "AgeGroupDisp_Name",
            case when coalesce(EPLDTI.Org_id,PDORP.Org_id) IS NOT NULL then 'true' else 'false' end as "OrgExist",
            EPLDTI.EvnPLDispTeenInspection_VizitCount as "EvnPLDispTeenInspection_VizitCount",
            IsFinish.YesNo_Name as "EvnPLDispTeenInspection_IsFinish",
            HK.HealthKind_Name as "EvnPLDispTeenInspection_HealthKind_Name",
            IsTwoStage.YesNo_Name as "EvnPLDispTeenInspection_IsTwoStage",
            case when PDORP.PersonDispOrp_id is not null then 'true' else 'false' end as "EvnPLDispTeenInspection_hasDirection",
            to_char(EPLDTI.EvnPLDispTeenInspection_setDT, 'DD.MM.YYYY') as "EvnPLDispTeenInspection_setDate",
            to_char(EPLDTI.EvnPLDispTeenInspection_disDT, 'DD.MM.YYYY') as "EvnPLDispTeenInspection_disDate",
            to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
            case when ecp.EvnCostPrint_IsNoPrint = 2 then 'Отказ от справки' when ecp.EvnCostPrint_IsNoPrint = 1 then 'Справка выдана' else '' end as "EvnCostPrint_IsNoPrintText",
            EPLDTI.EvnPLDispTeenInspection_IsSigned as "EvnPLDispTeenInspection_IsSigned",
            to_char(EPLDTI.EvnPLDispTeenInspection_signDT, 'DD.MM.YYYY') as "EvnPLDispTeenInspection_signDT"
        ,coalesce(UC.UslugaComplex_Code || '. ','') || UC.UslugaComplex_Name as "UslugaComplex_Name"
                ,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
                
         dbo.v_PersonState PS  inner join v_EvnPLDispTeenInspection EPLDTI on PS.Person_id = EPLDTI.Person_id 
            inner join v_Lpu L on L.Lpu_id = EPLDTI.Lpu_id
            left join v_EvnCostPrint ecp on ecp.Evn_id = EPLDTI.EvnPLDispTeenInspection_id
            left join YesNo IsFinish on IsFinish.YesNo_id = coalesce(EPLDTI.EvnPLDispTeenInspection_IsFinish, 1)
            left join YesNo IsTwoStage on IsTwoStage.YesNo_id = coalesce(EPLDTI.EvnPLDispTeenInspection_IsTwoStage, 1)
            left join v_AssessmentHealth AH on AH.EvnPLDisp_id = EPLDTI.EvnPLDispTeenInspection_id
            left join v_HealthGroupType HGT on HGT.HealthGroupType_id = AH.HealthGroupType_id
            left join v_HealthKind HK on HK.HealthKind_id = AH.HealthKind_id
            left join v_Sex Sex on Sex.Sex_id = PS.Sex_id
            left join v_Address UAdd on UAdd.Address_id = ps.UAddress_id
            left join v_Address PAdd on PAdd.Address_id = ps.PAddress_id
            left join v_PersonDispOrp PDORP on PDORP.PersonDispOrp_id = EPLDTI.PersonDispOrp_id
            left join v_AgeGroupDisp AGD on AGD.AgeGroupDisp_id = coalesce(EPLDTI.AgeGroupDisp_id, PDORP.AgeGroupDisp_id)
        
                left join lateral(
                    select UslugaComplex_id
                    from v_EvnUslugaDispDop
                    where EvnUslugaDispDop_IsVizitCode = 2
                      and EvnUslugaDispDop_pid = EPLDTI.EvnPLDispTeenInspection_id
                    limit 1
                ) as euddvizit on true
                left join v_UslugaComplex UC on uc.UslugaComplex_id = euddvizit.UslugaComplex_id
            
                         WHERE 
                        (1 = 1) and EPLDTI.EvnPLDispTeenInspection_setDT >= '2023-01-01'::date  and EPLDTI.EvnPLDispTeenInspection_setDT < '2023-12-31'::date + interval '1 day' and EPLDTI.EvnPLDispTeenInspection_disDT >= '2023-04-01'::date  and EPLDTI.EvnPLDispTeenInspection_disDT < '2023-06-30'::date + interval '1 day'  and coalesce(EPLDTI.EvnPLDispTeenInspection_IsFinish, 1) = '2'  and coalesce(EPLDTI.DispClass_id, 6) = '10'  and PS.Person_BirthDay >= (select dt from mv) - interval '1 year' * 18
         
                        
                
                        ORDER BY
                            
                        
                        PS.Person_SurName,
                        PS.Person_FirName,
                        PS.Person_SecName
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
