create temp table timeTable on commit drop as 
	with 
		ph as (
			SELECT 
				q.*,
				row_number() over(partition by q.TimeTableMedService_id order by q.TimeTableMedServiceHist_insDT desc) rn
			FROM (
				SELECT 
				   ttms.TimeTableMedService_id, 
				   ttms.TimetableMedService_begTime,
				   ttms.MedService_id,
				   ttms.RecMethodType_id, 
				   hist.TimeTableMedServiceHist_id,
				   hist.TimeTableMedServiceHist_insDT, 
				   hist.TimeTableActionType_id, 
				   case when EvnStatus.EvnStatus_SysNick = 'Serviced' then 1 else 0 end visited,
				   case when Evn_statusDate >= $1 then 1 else 0 end updated,
				   hist.pmUser_updID UserId,
				   ttms.Person_id,
				   EvnDirection.UslugaComplex_did,
				   Evn.Evn_pid
				FROM (SELECT cast($2::timestamp - interval '5 day' as date) firstDt) period 
				INNER JOIN dbo.TimeTableMedService ttms   ON ttms.TimetableMedService_begTime >= period.firstDt  
				INNER JOIN dbo.TimeTableMedServiceHist hist   ON hist.TimeTableMedService_id = ttms.TimeTableMedService_id
				INNER JOIN dbo.v_EvnDirection_all EvnDirection  ON EvnDirection.EvnDirection_id = ttms.EvnDirection_id 
				INNER JOIN dbo.DirType dt   ON dt.DirType_id = EvnDirection.DirType_id AND dt.DirType_Code = 9
				INNER JOIN dbo.Evn   ON Evn.Evn_id = EvnDirection.EvnDirection_id
				LEFT JOIN dbo.EvnStatus   ON EvnStatus.EvnStatus_id =  Evn.EvnStatus_id 
				WHERE hist.TimeTableActionType_id in (2,3)
				  AND ttms.TimetableMedService_deleted = 1
				  AND hist.TimeTableMedServiceHist_deleted = 1
				UNION 
				SELECT 
				   ttms.TimeTableMedService_id, 
				   ttms.TimetableMedService_begTime,
				   ttms.MedService_id,
				   ttms.RecMethodType_id, 
				   hist.TimeTableMedServiceHist_id,
				   hist.TimeTableMedServiceHist_insDT, 
				   hist.TimeTableActionType_id, 
				   case when EvnStatus.EvnStatus_SysNick = 'Serviced' then 1 else 0 end visited,
				   case when Evn_statusDate >= $3 then 1 else 0 end updated,
				   hist.pmUser_updID UserId,
				   ttms.Person_id,
				   EvnDirection.UslugaComplex_did,
				   Evn.Evn_pid
				FROM (SELECT $4 ::timestamp  firstDt) period 
				INNER JOIN dbo.Evn   ON Evn.Evn_statusDate >= firstDt AND Evn.Evn_deleted = 1
				INNER JOIN dbo.v_EvnDirection_all  EvnDirection  ON Evn.Evn_id = EvnDirection.EvnDirection_id 
				INNER JOIN dbo.DirType dt   ON dt.DirType_id = EvnDirection.DirType_id AND dt.DirType_Code = 9
				INNER JOIN dbo.EvnStatus   ON EvnStatus.EvnStatus_id = Evn.EvnStatus_id AND EvnStatus.EvnStatus_SysNick = 'Serviced'
				INNER JOIN dbo.TimeTableMedService ttms   ON EvnDirection.EvnDirection_id = ttms.EvnDirection_id 
				INNER JOIN dbo.TimeTableMedServiceHist hist   ON hist.TimeTableMedService_id = ttms.TimeTableMedService_id
				WHERE hist.TimeTableActionType_id in (2,3)
				  AND ttms.TimetableMedService_deleted = 1
				  AND hist.TimeTableMedServiceHist_deleted = 1
			) q		
		)
	SELECT
		ph0.TimeTableMedService_id, 
		ph0.TimetableMedService_begTime,
		ph0.MedService_id,
		ph0.RecMethodType_id, 
		ph0.TimeTableMedServiceHist_insDT, 
		ph0.TimeTableActionType_id,
		ph0.TimeTableMedServiceHist_id,
		ph0.UserId,
		ph0.Person_id,
		ph0.visited,
		ph0.updated,
		ph0.UslugaComplex_did,
		ph0.Evn_pid,
		row_number() over(partition by ph0.TimeTableMedService_id order by ph0.TimeTableMedServiceHist_insDT desc) rn
		 
	FROM ph ph0
	LEFT JOIN ph ph1 ON ph1.TimeTableMedService_id = ph0.TimeTableMedService_id and ph1.rn = ph0.rn-1 and ph1.TimeTableMedServiceHist_insDT > $5
	WHERE ph0.TimeTableMedServiceHist_insDT >= $6 or ph1.TimeTableMedService_id is not null or ph0.rn = 1
