
			--variables
			with mv as (
				select
				dbo.tzGetDate() as dt
			),
			--end variables
		
				-- addit with
			    reg as (
                	select dbo.getregion() as region_id
            	)
				-- end addit with
			
            select
				-- select
		
				distinct
			
			EDPS.EvnDiagPS_id as "DZ_ID",
			EPS.EvnPS_id as "GSP_ID",
			PS.PersonEvn_id as "PCT_ID",
			PS.Person_id as "P_ID",
			(case
				when EDPS.DiagSetType_id = 1 then 1
				when EDPS.DiagSetType_id = 2 then 2
				when EDPS.DiagSetType_id = 3 then 3
			else 0
			end) as "KTO",
			Lpu.Org_Code as "LPUK",
			Lpu.Lpu_Name as "LPU",
			LS.LpuSection_Code as "OTDK",
			LS.LpuSection_Name as "OTD",
			rtrim(coalesce(to_char(EDPS.EvnDiagPS_setDT, 'DD.MM.YYYY'),'')) as "DZ_DATA",
			DSC.DiagSetClass_Name as "DZ_W",
			DST.DiagSetType_Name as "DZ_T",
			Diag.Diag_Code as "DZ_DZ"
		
			-- end select
			from
				-- from
		 v_PersonState PS  inner join v_EvnPS EPS on EPS.Person_id = PS.Person_id
							and EPS.Lpu_id = '13100922'
				inner join v_EvnSection as firstProfileES on firstProfileES.EvnSection_pid = EPS.EvnPS_id
					and firstProfileES.EvnSection_Index = 1
					and firstProfileES.Lpu_id = '13100922'
			
			left join v_EvnSection ESEC on ESEC.EvnSection_pid = EPS.EvnPS_id and ESEC.Lpu_id = '13100922'
			inner join v_EvnDiagPS EDPS on EDPS.EvnDiagPS_pid = EPS.EvnPS_id or EDPS.EvnDiagPS_pid = ESEC.EvnSection_id
			left join DiagSetClass DSC on DSC.DiagSetClass_id = EDPS.DiagSetClass_id
			left join DiagSetType DST on DST.DiagSetType_id = EDPS.DiagSetType_id 
			left join Diag on Diag.Diag_id = EDPS.Diag_id
			left join v_Lpu Lpu on (
				(Lpu.Lpu_id = EPS.Lpu_did and EDPS.DiagSetType_id = 1) or
				(Lpu.Lpu_id = EPS.Lpu_id and EDPS.DiagSetType_id = 2) or
				(Lpu.Lpu_id = ESEC.Lpu_id and EDPS.DiagSetType_id = 3)
			)
			left join v_LpuSection LS on (
				(LS.LpuSection_id = EPS.LpuSection_did and EDPS.DiagSetType_id = 1) or
				(LS.LpuSection_id = EPS.LpuSection_pid and EDPS.DiagSetType_id = 2) or
				(LS.LpuSection_id = ESEC.LpuSection_id and EDPS.DiagSetType_id = 3)
			)
		
			left join lateral(
				select ServiceEvnStatus_id
				from v_ServiceEvnHist
				where Evn_id = EPS.EvnPS_id
				  and ServiceEvnList_id = 1
				order by ServiceEvnHist_id desc
				limit 1
			) as SEH1 on true
			left join v_ServiceEvnStatus SES1 on SES1.ServiceEvnStatus_id = SEH1.ServiceEvnStatus_id
		
						-- end from
					where
						-- where
						(1 = 1) and EPS.EvnPS_setDate >= '2021-01-01' and EPS.EvnPS_setDate <= '2021-12-31' and firstProfileES.LpuSection_id = '99560021875'
						-- end where
				
