select distinct
		Evn.EvnPS_id
		from dbo.v_EvnPS Evn  
		inner join dbo.v_Person Person   on Evn.Person_id = Person.Person_id
		inner join dbo.v_Lpu Lpu   on Evn.Lpu_id = Lpu.Lpu_id
		inner join dbo.v_EvnSection EvnSection   on Evn.EvnPs_id=EvnSection.EvnSection_pid
		inner join dbo.v_MedStaffFact msf   on msf.MedStaffFact_id=EvnSection.MedStaffFact_id
		where (1 = 1)
		-- фильтр по ЛПУ
		and (Lpu.Lpu_id in (0) or 0 = 0)
		
		--фильтр по подразделению
		and (msf.LpuBuilding_id in (0) or 0 = 0)
		
		--фильтр по диагнозу 
		and (Evn.Diag_id not in (select Diag_id from v_Diag where (
		Diag_pid in (0) or Diag_id in (0)
		or Diag_pid in (select Diag_id from v_Diag where Diag_pid in (0))))
		or Evn.Diag_id is null
		or 0 = 0 )
		
		--фильтр по врачу
		and (msf.MedStaffFact_id in (0) or 0 = 0)
		
		--фильтр по специальности врача
		and (msf.MedSpecOms_id in (0) or 0 = 0)
		
		and Evn.EvnPS_disDT is not null
		and Evn.LeaveType_id is not null
	 
		and Evn.EvnPS_updDT between $1 and $2
