Hi -- I have inherited a sql statement comprising of two subqueries (WeeklyAssignment and MasterWeeklyAssignment) and a UNION join. Against our database the below query returns two rows (values 0.5 and 0.75) as the particular resource is working on two projects. How do I total these so that I only ever get one row returned, which in this case should be 1.25 (0.5 + 0.75)? I have tried changing the group by to no avail - I'm not sure how to total when there are named sub queries. Thanks!
SELECT SUM(WORKDEMANDFTE) FROM ( SELECT dbo.MSP_EpmResource_UserView.ResourceName ,ISNULL(dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork, 0) / 8 / dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Working days in month_Double] AS [WORKDEMANDFTE] ,dbo.MSP_EpmProject_UserView.ProjectName ,DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay) AS AssignmentMonth ,DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay) AS AssignmentYear ,dbo.MSP_TimeByDay_OlapView.CalendarMemberNameYear ,dbo.MSP_EpmResource_UserView.ResourceUID FROM dbo.MSP_EpmResource_UserView INNER JOIN dbo.MSP_EpmAssignment_UserView INNER JOIN dbo.MSP_EpmAssignmentByDay_UserView ON dbo.MSP_EpmAssignment_UserView.AssignmentUID = dbo.MSP_EpmAssignmentByDay_UserView.AssignmentUID AND dbo.MSP_EpmAssignment_UserView.ProjectUID = dbo.MSP_EpmAssignmentByDay_UserView.ProjectUID ON dbo.MSP_EpmResource_UserView.ResourceUID = dbo.MSP_EpmAssignment_UserView.ResourceUID INNER JOIN dbo.MSP_EpmProject_UserView ON dbo.MSP_EpmAssignment_UserView.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID AND dbo.MSP_EpmAssignmentByDay_UserView.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID INNER JOIN dbo.MSP_TimeByDay_OlapView ON dbo.MSP_EpmAssignmentByDay_UserView.TimeByDay = dbo.MSP_TimeByDay_OlapView.TimeByDay INNER JOIN dbo.MSP_EpmTask ON dbo.MSP_EpmAssignment_UserView.TaskUID = dbo.MSP_EpmTask.TaskUID INNER JOIN dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE ON dbo.MSP_EpmResource_UserView.[Primary Role] = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Primary Role_String] AND DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay) = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Month_String] AND DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay) = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Year_String] AND (dbo.MSP_EpmProject_UserView.[Project Status] = 'Open') AND (dbo.MSP_EpmResource_UserView.ResourceUID = '8CB5EADB-B2C0-4BAF-A938-40AC81F9C4FB') AND (DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay)) = '2014' AND (DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay)) = 'February' AND (dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork > 0) ) AS WeeklyAssignment INNER JOIN ( SELECT dbo.MSP_EpmProject.ProjectName AS MProject ,dbo.MSP_EpmTask.TaskName AS ChildProject FROM dbo.MSP_EpmProject INNER JOIN dbo.MSP_EpmTask ON dbo.MSP_EpmProject.ProjectUID = dbo.MSP_EpmTask.ProjectUID WHERE (dbo.MSP_EpmTask.TaskIsProjectSummary = 0) ) AS MasterProjectDetails ON WeeklyAssignment.ProjectName = MasterProjectDetails.ChildProject UNION SELECT SUM(WORKDEMANDFTE) FROM ( SELECT dbo.MSP_EpmResource_UserView.ResourceName ,dbo.MSP_EpmAssignmentByDay_UserView.TimeByDay AS TimeByDay ,dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork AS FORECASTWORK ,ISNULL(dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork, 0) / 8 / dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Working days in month_Double] AS [WORKDEMANDFTE] ,dbo.MSP_EpmProject_UserView.ProjectName ,DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay) AS AssignmentMonth ,DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay) AS AssignmentYear ,dbo.MSP_TimeByDay_OlapView.CalendarMemberNameYear ,dbo.MSP_EpmResource_UserView.ResourceUID FROM dbo.MSP_EpmResource_UserView INNER JOIN dbo.MSP_EpmAssignment_UserView INNER JOIN dbo.MSP_EpmAssignmentByDay_UserView ON dbo.MSP_EpmAssignment_UserView.AssignmentUID = dbo.MSP_EpmAssignmentByDay_UserView.AssignmentUID AND dbo.MSP_EpmAssignment_UserView.ProjectUID = dbo.MSP_EpmAssignmentByDay_UserView.ProjectUID ON dbo.MSP_EpmResource_UserView.ResourceUID = dbo.MSP_EpmAssignment_UserView.ResourceUID INNER JOIN dbo.MSP_EpmProject_UserView ON dbo.MSP_EpmAssignment_UserView.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID AND dbo.MSP_EpmAssignmentByDay_UserView.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID INNER JOIN dbo.MSP_TimeByDay_OlapView ON dbo.MSP_EpmAssignmentByDay_UserView.TimeByDay = dbo.MSP_TimeByDay_OlapView.TimeByDay INNER JOIN dbo.MSP_EpmTask ON dbo.MSP_EpmAssignment_UserView.TaskUID = dbo.MSP_EpmTask.TaskUID INNER JOIN dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE ON dbo.MSP_EpmResource_UserView.[Primary Role] = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Primary Role_String] AND DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay) = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Month_String] AND DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay) = DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Year_String] AND (dbo.MSP_EpmProject_UserView.[Project Status] = 'Open') AND (dbo.MSP_EpmResource_UserView.ResourceUID = '8CB5EADB-B2C0-4BAF-A938-40AC81F9C4FB') AND (DATENAME(YEAR, MSP_EpmAssignmentByDay_UserView.TimeByDay)) = '2014' AND (DATENAME(MONTH, MSP_EpmAssignmentByDay_UserView.TimeByDay)) = 'February' AND (dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork > 0) ) AS MasterWeeklyAssignment GROUP BY ProjectName