Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Union Query

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

791 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