USE [EnVisage]
GO

/****** Object:  View [dbo].[VW_Skill2Resource]    Script Date: 3/15/2016 5:56:41 PM ******/
DROP VIEW [dbo].[VW_Skill2Resource]
GO

/****** Object:  View [dbo].[VW_Skill2Resource]    Script Date: 3/15/2016 5:56:41 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE view [dbo].[VW_Skill2Resource] AS
	SELECT A.*, ISNULL(D.Id, A.SkillId) AS SkillGroupId, 
		CAST((CASE WHEN (D.Id IS NULL) THEN 1 ELSE 0 END) AS bit) AS AssignedToGroup,
		E.Id AS TeamId, E.CompanyId, abs(datediff(D, dateadd(y, -1, GETUTCDATE()), A.effectivedate)) as PastYearDateOffset,
		abs(datediff(D, A.effectivedate, dateadd(y, 1, GETUTCDATE()))) as FutureYearDateOffset
	FROM Skill2Resource A 
		INNER JOIN Skill B ON (B.Id = A.SkillId)
		INNER JOIN VW_TeamResource C ON (C.Id = A.ResourceId)
		LEFT JOIN Skill D ON (D.Id = B.ParentId)
		LEFT JOIN Team E ON (E.Id = C.TeamId)

GO


