Solved

SQL Union Query

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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