
				
			with mv as (
				select
				dbo.tzGetDate() as dt
			),
			
				
			    reg as (
                	select dbo.getregion() as region_id
            	), 
						PCSD1 as (
							select
								max(Personcard_id) as Personcard_id,
								Person_id
							from
								v_PersonCard_all pc
							where
								pc.Lpu_id = '13101875'
							group by Person_id
						)
				
				SELECT 
		
			PS.Person_id as "Person_id",
			RTRIM(PS.Person_SurName) || ' ' || coalesce(PS.Person_FirName, '') || ' ' || coalesce(PS.Person_SecName, '') as "Person_FIO",
			to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday",
			sex.Sex_Name as "Person_Sex",
			to_char(IsPersonDopDispPassed.EvnPLDisp_setDate, 'DD.MM.YYYY') as "EvnPLDisp_setDate",
			to_char(IsPersonDopDispPassed.EvnPLDisp_disDate, 'DD.MM.YYYY') as "EvnPLDisp_disDate",
			2 as "IsChecked"
		
			
				FROM  
				
		
                	    PCSD1 
                	    inner join v_PersonState_all PS on PCSD1.person_id=PS.person_id
					
			left join Sex on Sex.Sex_id = PS.Sex_id
			left join lateral(
				select
					epld.EvnPLDisp_id,
					epld.EvnPLDisp_setDate,
					epld.EvnPLDisp_disDate
				from v_EvnPLDisp epld 
				where epld.Person_id = PS.Person_id
				  and date_part('year', epld.EvnPLDisp_setDate) = 2022
				  and epld.DispClass_id = '1'
				limit 1
			) as IsPersonDopDispPassed on true
		 left join v_PersonPrivilege PP on PP.Person_id = PS.Person_id
			left join lateral(
				select ppl.PlanPersonList_id
				from
					v_PlanPersonList ppl 
					inner join v_PersonDopDispPlan pddp on pddp.PersonDopDispPlan_id = ppl.PersonDopDispPlan_id
					left join v_PlanPersonListStatus pddps on pddps.PlanPersonListStatus_id = PPL.PlanPersonListStatus_id
				where ppl.Person_id = PS.Person_id
				  and pddp.PersonDopDispPlan_Year = 2022
				  and pddp.DispClass_id = '1'
				  and coalesce(PDDPS.PlanPersonListStatusType_id, 1) <> 4
				limit 1
			) as IsPersonDopDispPlanned on true
		
						 WHERE 
						(1 = 1)
					and PS.Person_BirthDay <= '2004-12-31'
					and (
							(dbo.Age2(PS.Person_BirthDay, CAST('2022-12-31' as timestamp)) % 3 = 0) or
							(PP.PersonPrivilege_id is not null and PP.PrivilegeType_id in (10,11,50))
							
							or
							(dbo.Age2(PS.Person_BirthDay, CAST('2022-12-31' as timestamp)) >= 40)
						
					) and IsPersonDopDispPlanned.PlanPersonList_id is null  and IsPersonDopDispPassed.EvnPLDisp_id is null  and PS.Person_DeadDT is null and (PS.Person_BirthDay >= dateadd('year', -99, tzgetdate())) and (PS.Person_BirthDay <= dateadd('year', -18, tzgetdate())) 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::timestamp > (select dt from mv)
									)
							 and PC.Lpu_id = '13101875' and PC.Lpu_id = '13101875' and PC.LpuAttachType_id = '1' and LR.LpuRegion_id = 890101000000028 and COALESCE(LR.LpuRegionType_id,PC.LpuRegionType_id) = '1'
						limit 1
					)) 
						-- end where
				
					group by
						PS.Person_id,
						PS.Person_SurName,
						PS.Person_FirName,
						PS.Person_SecName,
						Sex.Sex_Name,
						PS.Person_Birthday,
						IsPersonDopDispPassed.EvnPLDisp_setDate,
						IsPersonDopDispPassed.EvnPLDisp_disDate
					
				
						ORDER BY 
							
						
						PS.Person_SurName,
						PS.Person_FirName,
						PS.Person_SecName
						
				OFFSET 0 ROWS		
				FETCH NEXT 5000000 ROWS ONLY 
				
