
            --variables
            with mv as (
                select
                dbo.tzGetDate() 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)
        
                        -- end from
                    where
                        -- where
                        (1 = 1) and EPLDTI.EvnPLDispTeenInspection_setDate >= '2022-01-01'::timestamp  and EPLDTI.EvnPLDispTeenInspection_setDate <= '2022-12-31'::timestamp  and EPLDTI.EvnPLDispTeenInspection_disDate >= '2022-01-01'::timestamp  and EPLDTI.EvnPLDispTeenInspection_disDate <= '2022-02-17'::timestamp  and coalesce(EPLDTI.DispClass_id, 6) = '10' 
                and (
                    exists (
                        select msf1.EvnVizitDispDop_id
                        from
                            v_EvnVizitDispDop msf1
                             left join v_LpuSection ls1 on ls1.LpuSection_id = msf1.LpuSection_id 
                        where msf1.EvnVizitDispDop_pid = EPLDTI.EvnPLDispTeenInspection_id  and ls1.LpuBuilding_id = '520101000000307'  
                        limit 1
                    ) or
                    exists (
                        select msf2.EvnUslugaDispDop_id
                        from
                            v_EvnUslugaDispDop msf2
                             left join v_LpuSection ls3 on ls3.LpuSection_id = msf2.LpuSection_uid 
                        where msf2.EvnUslugaDispDop_pid = EPLDTI.EvnPLDispTeenInspection_id  and ls3.LpuBuilding_id = '520101000000307'  
                        limit 1
                    )
                )
             and dbo.Age(PS.Person_BirthDay, (select dt from mv)) <= 18
                        -- end where
                
                    
                
