USE [EnVisage]
GO

IF EXISTS(SELECT * FROM sys.views WHERE Name = N'VW_NonProjectTimeTeamAllocation')
BEGIN
	DROP VIEW VW_NonProjectTimeTeamAllocation
END
GO

CREATE VIEW VW_NonProjectTimeTeamAllocation AS
SELECT A.NonProjectTimeId, A.TeamId, R2T.Id as PeopleResourceId, B.WeekEndingDate, B.HoursOff, R2T.ExpenditureCategoryId,
	T.NonProjectTimeCategoryId
	FROM NonProjectTime2Team A
	INNER JOIN NonProjectTimeTeamAllocation B ON (B.NonProjectTime2TeamId = A.Id)
	INNER JOIN NonProjectTime T ON (T.Id = A.NonProjectTimeId)
	INNER JOIN VW_TeamResource R2T ON (R2T.TeamId = A.TeamId) AND (R2T.TeamStartDate < B.WeekEndingDate) AND
		((R2T.TeamEndDate IS NULL) OR (R2T.TeamEndDate >= B.WeekEndingDate))
GO


IF EXISTS(SELECT * FROM sys.views WHERE Name = N'VW_NonProjectTimeAllocation')
BEGIN
	EXECUTE sp_refreshview N'VW_NonProjectTimeAllocation'
END
GO