Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Including a SUM within a grouped UNION query

$
0
0

Hi -- I have inherited a SSRS (Project Server Resource) report comprising two queries with a UNION. There is a requirement to add ONE extra column (MonthlyTotal) which SUMS the total WORKDEMANDFTE for each RESOURCE per MONTH. 

However, as soon as grouping and additional lines (the lines with comments) are included the following error is generated:

Column 'dbo.MSP_EpmResource_UserView.ResourceName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

ResourceName is included so I cannot see why this won't compile or run; I have tried using the subquery prexfix, full column names etc. 

Any advice appreciated?

ALTER PROCEDURE [dbo].[SP_OverallocatedNamedResource] @StartDate DATETIME
	,@EndDate DATETIME
AS
BEGIN
	SELECT WeeklyAssignment.ResourceName
		,WeeklyAssignment.TimeByDay
		,WeeklyAssignment.FORECASTWORK
		,WeeklyAssignment.WORKDEMANDFTE
		,WeeklyAssignment.ProjectName
		,WeeklyAssignment.AssignmentMonth
		,WeeklyAssignment.AssignmentYear
		,WeeklyAssignment.CalendarMemberNameYear
		,WeeklyAssignment.ResourceUID
	--,WeeklyAssignment.MonthlyTotal
	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
		--,SUM(ISNULL(dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork, 0) / 8 / dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Working days in month_Double]) AS MonthlyTotal
		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] WHERE (
				dbo.MSP_EpmAssignmentByDay_UserView.TimeByDay BETWEEN CONVERT(DATETIME, @StartDate)
					AND CONVERT(DATETIME, @EndDate)
				)
			AND (dbo.MSP_TimeByDay_OlapView.CalendarMemberKeyWeek <> 53)
			AND (dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork > 0)
			AND (
				dbo.MSP_EpmResource_UserView.[Primary Role] IN (
					'Application Support'
					,'Architect'
					)
				)
			AND (
				dbo.MSP_EpmResource_UserView.ResourceName NOT IN (
					'Application Support'
					,'Architect'
					)
				)
		) 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
	GROUP BY AssignmentMonth
		,ResourceName
		,TimeByDay
		,FORECASTWORK
		,WORKDEMANDFTE
		,ProjectName
		,AssignmentYear
		,CalendarMemberNameYear
		,ResourceUID
	--,MonthlyTotal
	UNION
	SELECT MasterWeeklyAssignment.ResourceName
		,MasterWeeklyAssignment.TimeByDay
		,MasterWeeklyAssignment.FORECASTWORK
		,MasterWeeklyAssignment.WORKDEMANDFTE
		,MasterWeeklyAssignment.ProjectName
		,MasterWeeklyAssignment.AssignmentMonth
		,MasterWeeklyAssignment.AssignmentYear
		,MasterWeeklyAssignment.CalendarMemberNameYear
		,MasterWeeklyAssignment.ResourceUID
	--,MasterWeeklyAssignment.MonthlyTotal
	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
		--,SUM(ISNULL(dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork, 0) / 8 / dbo.DATAMART_WSSLIST_RESOURCE_CAPACITY_FTE.[wss_Working days in month_Double]) AS MonthlyTotal
		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] WHERE (
				dbo.MSP_EpmAssignmentByDay_UserView.TimeByDay BETWEEN CONVERT(DATETIME, @StartDate)
					AND CONVERT(DATETIME, @EndDate)
				)
			AND (dbo.MSP_TimeByDay_OlapView.CalendarMemberKeyWeek <> 53)
			AND (dbo.MSP_EpmAssignmentByDay_UserView.AssignmentWork > 0)
			AND (
				dbo.MSP_EpmResource_UserView.[Primary Role] IN (
					'Application Support'
					,'Architect'
					)
				)
			AND (
				dbo.MSP_EpmResource_UserView.ResourceName NOT IN (
					'Application Support'
					,'Architect'
					)
				)
		) AS MasterWeeklyAssignment
	GROUP BY AssignmentMonth
		,ResourceName
		,TimeByDay
		,FORECASTWORK
		,WORKDEMANDFTE
		,ProjectName
		,AssignmentYear
		,CalendarMemberNameYear
		,ResourceUID
		--,MonthlyTotal
END


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>