Solved

SQL Union Query

Posted on 2014-04-19
2
306 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
[X]
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
2 Comments
 
LVL 34

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 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