USE [EnVisage]
GO

/****** Object:  View [dbo].[SC_TotalCGSeatsAcrossProject]    Script Date: 01.08.2014 11:35:20 ******/
if exists (select 1 from sys.views where name like 'SC_TotalSeatsAcrossExpCatsCln')
begin
	DROP VIEW [dbo].[SC_TotalSeatsAcrossExpCatsCln]
end
GO


CREATE VIEW [dbo].[SC_TotalSeatsAcrossExpCatsCln]
AS
	SELECT SD.ParentID AS ScenarioObjectID,
	       SD.WeekEndingDate AS WeekEndingDate,
	       SUM(SD.Quantity) AS Quantity,
	       SUM(SD.Cost) AS Cost,
	       EC.CGEFX AS CGEFX,
	       SC.Type AS ScenarioType,
	       SC.Name AS ScenarioName,
	       SH.ID AS ShowObjectID,
	       SH.Name AS ShowName,
	       SH.TypeID AS ShowTypeObjectID,
	       LS.ID AS ShowStatusObjectID,
	       LS.Name AS ShowStatusName,
	       SH.Probability,
	       SH.Color AS ShowColor,
	       SC.Color AS ScenarioColor,
	       EC.ID AS ExpenditureCatObjectID,
	       SC.Status,
	       SC.SystemAttributeObjectID	
	  FROM [Status] LS, Project SH, Scenario SC, Expenditure_Category EC, Scenario_Detail SD
	 WHERE (SC.Type IN (5, 6, 7, 9, 11))
	   AND (LS.ID = SH.StatusID)
	   AND (SC.ParentID = SH.ID)
	   AND (SC.ID = SD.ParentID)
	   AND (EC.ID = SD.ExpenditureCategoryID)
	 GROUP BY SD.WeekEndingDate, SD.ParentID, 
		  SC.Type, SC.Name, SC.Color, SH.Probability, 
		  SC.Status, SC.SystemAttributeObjectID,
		  SH.ID, SH.Name, SH.TypeID, SH.Color,
		  EC.CGEFX, EC.ID,
		  LS.ID, LS.Name
	UNION
	SELECT SD.ParentID AS ScenarioObjectID,
	       SD.WeekEndingDate AS WeekEndingDate,
	       SUM(SD.Quantity) AS Quantity,
	       SUM(SD.Cost) AS Cost,
	       EC.CGEFX AS CGEFX,
	       SC.Type AS ScenarioType,
	       SC.Name AS ScenarioName,
	       SH.ID AS ShowObjectID,
	       SH.Name AS ShowName,
	       SH.TypeID AS ShowTypeObjectID,
	       LS.ID AS ShowStatusObjectID,
	       LS.Name AS ShowStatusName,
	       SH.Probability,
	       SH.Color AS ShowColor,
	       SC.Color AS ScenarioColor,
	       EC.ID AS ExpenditureCatObjectID,
	       CL.Status,
	       CL.SystemAttributeObjectID	
	  FROM [Status] LS, Project SH, Scenario SC, ScenarioCln CL, Expenditure_Category EC, 
	       Scenario_Detail SD
	 WHERE (SC.Type IN (2, 8, 10))
	   AND (LS.ID = SH.StatusID)
	   AND (SC.ParentID = SH.ID)
	   AND (SC.ID = CL.ID)
	   AND (SC.ID = SD.ParentID)
	   AND (EC.ID = SD.ExpenditureCategoryID)
	 GROUP BY SD.WeekEndingDate, SD.ParentID, 
		  SC.Type, SC.Name, SC.Color, SH.Probability, 
		  CL.Status, CL.SystemAttributeObjectID,
		  SH.ID, SH.Name, SH.TypeID, SH.Color,
		  EC.CGEFX, EC.ID,
		  LS.ID, LS.Name

GO