create temp table timeTable on commit drop as 
	with 
		ph as (
			SELECT 
				q.*,
				row_number() over(partition by q.TimeTableResource_id order by q.TimeTableResourceHist_insDT desc) rn
			FROM (
				SELECT 
				   ttms.TimeTableResource_id, 
				   ttms.TimetableResource_begTime,
				   ttms.Resource_id,
				   ttms.RecMethodType_id, 
				   hist.TimeTableResourceHist_id,
				   hist.TimeTableResourceHist_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,
				   EvnDirection.MedService_id
				FROM (SELECT cast($2::timestamp - interval '5 day' as date) firstDt) period
				INNER JOIN dbo.TimeTableResource ttms   ON ttms.TimetableResource_begTime  >= cast($3::timestamp - interval '5 day' as date) 
				INNER JOIN dbo.TimeTableResourceHist hist   ON hist.TimeTableResource_id = ttms.TimeTableResource_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.TimetableResource_deleted = 1
				  AND hist.TimeTableResourceHist_deleted = 1
				UNION
				SELECT 
				   ttms.TimeTableResource_id, 
				   ttms.TimetableResource_begTime,
				   ttms.Resource_id,
				   ttms.RecMethodType_id, 
				   hist.TimeTableResourceHist_id,
				   hist.TimeTableResourceHist_insDT, 
				   hist.TimeTableActionType_id, 
				   case when EvnStatus.EvnStatus_SysNick = 'Serviced' then 1 else 0 end visited,
				   case when Evn_statusDate >= $4 then 1 else 0 end updated,
				   hist.pmUser_updID UserId,
				   ttms.Person_id,
				   EvnDirection.UslugaComplex_did,
				   Evn.Evn_pid,
				   EvnDirection.MedService_id
				FROM (SELECT $5 ::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.TimeTableResource ttms   ON EvnDirection.EvnDirection_id = ttms.EvnDirection_id
				INNER JOIN dbo.TimeTableResourceHist hist   ON hist.TimeTableResource_id = ttms.TimeTableResource_id
				WHERE hist.TimeTableActionType_id in (2,3)
				  AND ttms.TimetableResource_deleted = 1
				  AND hist.TimeTableResourceHist_deleted = 1
			) q
		)
	SELECT
		ph0.TimeTableResource_id, 
		ph0.TimetableResource_begTime,
		ph0.Resource_id,
		ph0.RecMethodType_id, 
		ph0.TimeTableResourceHist_insDT, 
		ph0.TimeTableActionType_id,
		ph0.TimeTableResourceHist_id,
		ph0.UserId,
		ph0.Person_id,
		ph0.visited,
		ph0.updated,
		ph0.UslugaComplex_did,
		ph0.Evn_pid,
		ph0.MedService_id,
		row_number() over(partition by ph0.TimeTableResource_id order by ph0.TimeTableResourceHist_insDT desc) rn
		 
	FROM ph ph0
	LEFT JOIN ph ph1 ON ph1.TimeTableResource_id = ph0.TimeTableResource_id and ph1.rn = ph0.rn-1 and ph1.TimeTableResourceHist_insDT > $6
	WHERE ph0.TimeTableResourceHist_insDT >= $7 or ph1.TimeTableResource_id is not null or ph0.rn = 1
