USE [EnVisage]
GO

ALTER VIEW [dbo].[VW_ScenarioForecastTotalCost] AS
SELECT A.ParentID AS ForecastScenarioId, SUM(A.Cost) AS TotalCost, SUM(A.Quantity) TotalQuantity
FROM ScenarioDetail A
INNER JOIN VW_ScenarioRelations B ON (A.ParentID = B.ForecastScenarioId)
INNER JOIN VW_ScenarioActualsPeriod C ON (B.ActualsScenarioId = C.ScenarioId) AND (A.WeekEndingDate <= C.EndDate)
GROUP BY A.ParentID

GO