Solved

SQL Union Query

Posted on 2014-04-19
2
302 Views
Last Modified: 2014-04-19
I have a working query below:

SELECT ProjectManagers.PMName, Sum(CONVERT(int,Resource1Hours)) AS TotalHours
FROM (ProjectManagers INNER JOIN Projects ON ProjectManagers.PMID = Projects.PMID) INNER JOIN Schedule ON Projects.ProjectID = Schedule.ProjectID
GROUP BY PMName ORDER BY TotalHours DESC

There are two additional columns in the Schedule Table called Resource2Hours and Resource3Hours that I would like to UNION with this query and add to Resource1Hours to get the TotalHours.  The join to the Projects and ProjectManagers table to Group the totals by the Project Manager.  The Project Manager is not in the Schedule table - I need to get the Project Manager information from the Project table and then by the Project Manager Table.

I have the final query working in MSAccess, but having trouble converting to MS SQL.  Below is the working MSAccess query if it is of any help.

SELECT tmp.dbo_ProjectManagers.PMName, Sum(tmp.Hours) AS SumOfHours
FROM (SELECT dbo_ProjectManagers.PMName, Sum(Nz(val(Resource1Hours),0)) AS Hours
FROM (dbo_ProjectManagers INNER JOIN dbo_Projects ON dbo_ProjectManagers.PMID = dbo_Projects.PMID) INNER JOIN dbo_Schedule ON dbo_Projects.ProjectID = dbo_Schedule.ProjectID
GROUP BY dbo_ProjectManagers.PMName
UNION ALL
SELECT dbo_ProjectManagers.PMName, Sum(Nz(val(Resource2Hours),0)) AS Hours
FROM (dbo_ProjectManagers INNER JOIN dbo_Projects ON dbo_ProjectManagers.PMID = dbo_Projects.PMID) INNER JOIN dbo_Schedule ON dbo_Projects.ProjectID = dbo_Schedule.ProjectID
GROUP BY dbo_ProjectManagers.PMName
UNION ALL
SELECT dbo_ProjectManagers.PMName, Sum(Nz(val(Resource3Hours),0)) AS Hours
FROM (dbo_ProjectManagers INNER JOIN dbo_Projects ON dbo_ProjectManagers.PMID = dbo_Projects.PMID) INNER JOIN dbo_Schedule ON dbo_Projects.ProjectID = dbo_Schedule.ProjectID
GROUP BY dbo_ProjectManagers.PMName
)  AS tmp
GROUP BY tmp.PMName
ORDER BY Sum(tmp.Hours) DESC;

Thanks in advance,
Brian
0
Comment
Question by:MrBeedge
2 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40010466
E.g.

SELECT	tmp.dbo.ProjectManagers.PMName, 
	SUM(tmp.[Hours]) AS SumOfHours
FROM	(
	SELECT	dbo.ProjectManagers.PMName, 
		SUM(COALESCE(Resource1Hours, 0)) AS [Hours]
	FROM	dbo.ProjectManagers 
		INNER JOIN dbo.Projects ON dbo.ProjectManagers.PMID = dbo.Projects.PMID
		INNER JOIN dbo.Schedule ON dbo.Projects.ProjectID = dbo.Schedule.ProjectID
	GROUP BY dbo.ProjectManagers.PMName

	UNION ALL

	SELECT	dbo.ProjectManagers.PMName, 
		SUM(COALESCE(Resource2Hours ,0)) AS [Hours]
	FROM	dbo.ProjectManagers 
		INNER JOIN dbo.Projects ON dbo.ProjectManagers.PMID = dbo.Projects.PMID
		INNER JOIN dbo.Schedule ON dbo.Projects.ProjectID = dbo.Schedule.ProjectID
	GROUP BY dbo.ProjectManagers.PMName

	UNION ALL
	
	SELECT	dbo.ProjectManagers.PMName, 
		SUM(COALESCE(Resource3Hours ,0)) AS [Hours]
	FROM	dbo.ProjectManagers 
		INNER JOIN dbo.Projects ON dbo.ProjectManagers.PMID = dbo.Projects.PMID
		INNER JOIN dbo.Schedule ON dbo.Projects.ProjectID = dbo.Schedule.ProjectID
	GROUP BY dbo.ProjectManagers.PMName
	
	)  AS tmp
GROUP BY tmp.PMName
ORDER BY SUM(tmp.[Hours]) DESC;

Open in new window


But this should be sufficient:

SELECT	dbo.ProjectManagers.PMName, 
	SUM(COALESCE(Resource1Hours, 0)) + SUM(COALESCE(Resource2Hours ,0)) + SUM(COALESCE(Resource3Hours ,0)) AS [Hours]
FROM	dbo.ProjectManagers 
	INNER JOIN dbo.Projects ON dbo.ProjectManagers.PMID = dbo.Projects.PMID
	INNER JOIN dbo.Schedule ON dbo.Projects.ProjectID = dbo.Schedule.ProjectID
GROUP BY dbo.ProjectManagers.PMName
ORDER BY 2 DESC;

Open in new window

0
 

Author Closing Comment

by:MrBeedge
ID: 40010472
The simplified query worked great.  Thank you so much!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question