
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count(  * ) AS cnt 
            from
                -- 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
            
                        -- end from
                    where
                        -- where
                        (1 = 1) and EPLDTI.EvnPLDispTeenInspection_setDT >= '2023-01-01'::date  and EPLDTI.EvnPLDispTeenInspection_setDT < '2023-07-27'::date + interval '1 day' and HK.HealthKind_id = '3'  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
          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
                                left join v_MedStaffRegion MedStaffRegion on MedStaffRegion.LpuRegion_id = PC.LpuRegion_id
                                left join v_MedStaffFact msf on msf.MedStaffFact_id = MedStaffRegion.MedStaffFact_id and MedStaffRegion.Lpu_id = msf.Lpu_id
                                left join persis.Post p on p.id = msf.Post_id
                            
                        where
                            PC.Person_id = PS.Person_id
                                    and (
                                        PC.PersonCard_endDate is null
                                        or PC.PersonCard_endDate > (select dt from mv)
                                    )
                             and PC.Lpu_id = '150509' and PC.LpuAttachType_id = '1'
                        limit 1
                    )) 
                        -- end where
                
                    
                
