Solved

SQL Union Query

Posted on 2014-04-19
2
298 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 32

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now