
			--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
			
				EVizitPL.EvnVizitPL_pid as "EPL_ID",
				
				EVizitPL.EvnVizitPL_id as "EVZ_ID",
				EPL.EvnPL_NumCard as "NUMCARD",
				rtrim(coalesce(to_char(EVizitPL.EvnVizitPL_setDate, 'DD.MM.YYYY'),'')) as "SETDATE",
				EVizitPL.EvnVizitPL_setTime as "SETTIME",
				dbfvc.VizitClass_Code as "PERVVTOR",
				dbfls.LpuSection_Code as "LS_COD",
				rtrim(dbfls.LpuSection_Name) as "LS_NAM",
				dbfmp.MedPersonal_TabCode as "MP_COD",
				rtrim(dbfmp.Person_FIO) as "MP_FIO",
				dbfpt.PayType_Code as "PAY_COD",
				dbfvt.VizitType_Code as "VZT_COD",
				dbfst.ServiceType_Code as "SRT_COD",
				dbfpg.ProfGoal_code as "PRG_COD",
				dbfdiag.Diag_Code as "DZ_COD",
				rtrim(dbfdiag.Diag_Name) as "DZ_NAM",
				dbfdt.DeseaseType_Code as "DST_COD"
			
			-- end select
			from
				-- from
		
					v_EvnVizitPL EVizitPL 
					
					inner join v_EvnPL EPL on EVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  EVizitPL.Lpu_id = '3311071' 
					-- inner join Evn on Evn.Evn_id = EVizitPL.EvnVizitPL_pid and Evn.EvnClass_id = 3 and Evn.Evn_deleted = 1 and Evn.Lpu_id = '3311071'
					--	inner join v_PersonState PS on  EPL.Person_id = PS.Person_id 
					--	left join v_Polis pls on pls.Polis_id = ps.Polis_id  
										/*left join lateral(
						select
							EvnPLBase.EvnPLBase_IsFinish,
							EvnPLBase.EvnPLBase_VizitCount
						from v_EvnPLBase EvnPLBase
						where EvnPLBase.EvnPLBase_id = Evn.Evn_id
						limit 1
					) as EvnPLBase on true*/
					
				
						left join v_Diag as dbfdiag on dbfdiag.Diag_id=EVizitPL.Diag_id
						left join LpuSection as dbfls on dbfls.LpuSection_id=EVizitPL.LpuSection_id
						left join v_MedPersonal as dbfmp on dbfmp.MedPersonal_id = EVizitPL.MedPersonal_id and dbfmp.Lpu_id = EPL.Lpu_id
						left join PayType as dbfpt on dbfpt.PayType_id=EVizitPL.PayType_id
						left join VizitClass as dbfvc on dbfvc.VizitClass_id=EVizitPL.VizitClass_id
						left join VizitType as dbfvt on dbfvt.VizitType_id=EVizitPL.VizitType_id
						left join DeseaseType as dbfdt on dbfdt.DeseaseType_id=EVizitPL.DeseaseType_id
						left join ServiceType as dbfst on dbfst.ServiceType_id=EVizitPL.ServiceType_id
						left join ProfGoal as dbfpg on dbfpg.ProfGoal_id=EVizitPL.ProfGoal_id
					
								inner join v_PersonState PS on EVizitPL.Person_id = PS.Person_id
								left join v_Polis pls on pls.Polis_id = ps.Polis_id
							
			inner join v_LpuSection LS on LS.LpuSection_id = EPL.LpuSection_id
			inner join LpuUnit LU on LU.LpuUnit_id = LS.LpuUnit_id
			left join lateral(
				select ServiceEvnStatus_id
				from v_ServiceEvnHist
				where Evn_id = EPL.EvnPL_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 coalesce(dbfdiag.Diag_Code, '') not between 'F00.0' and 'F99' and coalesce(dbfdiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(dbfdiag.Diag_Code, '') not between 'A50.0' and 'A64' and EVizitPL.pmUser_insID = '392418131312' and EVizitPL.EvnVizitPL_updDT::date >= '2021-12-01'::date and EVizitPL.EvnVizitPL_updDT::date <= '2021-12-31'::date
						-- end where
				
						order by
							-- order by
							5
							-- end order by
					
