USE [EnVisage]
GO

/****** Object:  View [dbo].[VW_NonProjectTimeAllocation]    Script Date: 5/6/2016 6:57:38 PM ******/
DROP VIEW [dbo].[VW_NonProjectTimeAllocation]
GO

/****** Object:  View [dbo].[VW_NonProjectTimeAllocation]    Script Date: 5/6/2016 6:57:38 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE VIEW [dbo].[VW_NonProjectTimeAllocation] AS
WITH MixedAllocationData (NonProjectTimeId, PeopleResourceId, WeekEndingDate, HoursOff, ExpenditureCategoryId,
	NonProjectTimeCategoryId) AS
(
	SELECT NonProjectTimeId, PeopleResourceId, WeekEndingDate, HoursOff, ExpenditureCategoryId,
		NonProjectTimeCategoryId
	FROM VW_NonProjectTimeResourceAllocation
	UNION ALL
	SELECT NonProjectTimeId, PeopleResourceId, WeekEndingDate, HoursOff, ExpenditureCategoryId,
		NonProjectTimeCategoryId
	FROM VW_NonProjectTimeTeamAllocation
)
SELECT M.NonProjectTimeId, P.Name as NonProjectTimeName, M.PeopleResourceId, M.WeekEndingDate, ISNULL(SUM(M.HoursOff), 0) AS HoursOff, 
	M.ExpenditureCategoryId, M.NonProjectTimeCategoryId, Cat.Name as NonProjectTimeCategoryName, P.IsTeamMode AS IsTeamNonProjectTime 
FROM MixedAllocationData M
	INNER JOIN NonProjectTime P ON (M.NonProjectTimeId = P.Id)
	INNER JOIN NonProjectTimeCategory Cat on Cat.Id = M.NonProjectTimeCategoryId
GROUP BY M.NonProjectTimeId, P.Name, M.PeopleResourceId, M.WeekEndingDate, M.ExpenditureCategoryId, 
	M.NonProjectTimeCategoryId, Cat.Name, P.IsTeamMode

GO


