Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combining two queries – UNION with a JOIN

Author  Topic 

mgarret
Starting Member

14 Posts

Posted - 2013-06-10 : 08:54:27
Hi all—
I have to queries I need to combine with a left join and I am having trouble figuring out the syntax. I need to join the first query with a query that contains Unions. The queries need to by joined on File_NBR which is contained in vw_SBC_Employee_Info, vw_ADPFile and SBC_Best_Scores.

Query 1

SELECT
e1.File_NBR,
e1.Division,
e1.Department,
e1.Program,
e1.UNIT,
(e6.Mngr_FName + ' ' + e6.Mngr_LName) AS President,
(e5.Mngr_FName + ' ' + e5.Mngr_LName) AS VP,
(e4.Mngr_FName + ' ' + e4.Mngr_LName) AS AVP,
(e3.Mngr_FName + ' ' + e3.Mngr_LName) AS Director,
(e2.Mngr_FName + ' ' + e2.Mngr_LName) AS AD,
(e1.Mngr_FName + ' ' + e1.Mngr_LName) AS Supervisor,
(e1.First_Name + ' ' + e1.Last_Name) AS Case_Planner
FROM
[New_EEs].[dbo].[vw_SBC_Employee_Info] e1
JOIN
[New_EEs].[dbo].[vw_ADPFile] e2
on e1.Supervisor_Position_NBR=e2.POSITION_NBR
join
[New_EEs].[dbo].[vw_ADPFile] e3
on e2.Supervisor_Position_NBR=e3.POSITION_NBR
left join
[New_EEs].[dbo].[vw_ADPFile] e4
on e3.Supervisor_Position_NBR=e4.POSITION_NBR
left join
[New_EEs].[dbo].[vw_ADPFile] e5
on e4.Supervisor_Position_NBR=e5.POSITION_NBR
left join
[New_EEs].[dbo].[vw_ADPFile] e6
on e5.Supervisor_Position_NBR=e6.POSITION_NBR
order by e1.UNIT;

Query 2

SELECT
'Skill Rating' as Assessment_Type,
bs.File_NBR as ID,
bs.Skill_NBR,
bs.Best_Score as Score,
bs.Assesment_RND
FROM
[New_EEs].[dbo].[SBC_Best_Scores] bs
union
SELECT
sd.Assessment_Type,
sd.EXAMINEE_NBR as ID,
Skill_NBR,
sd.SCORE,
sd.SBC_RND
FROM
[New_EEs].[dbo].[vw_SBC_Score_Details]sd
order by ID,Assessment_Type;

Again I’m finding this tricky any assistance will be greatly aperciated!

Thanks-

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-10 : 09:00:28
See below
SELECT * FROM
(
SELECT e1.File_NBR ,
e1.Division ,
e1.Department ,
e1.Program ,
e1.UNIT ,
( e6.Mngr_FName + ' ' + e6.Mngr_LName ) AS President ,
( e5.Mngr_FName + ' ' + e5.Mngr_LName ) AS VP ,
( e4.Mngr_FName + ' ' + e4.Mngr_LName ) AS AVP ,
( e3.Mngr_FName + ' ' + e3.Mngr_LName ) AS Director ,
( e2.Mngr_FName + ' ' + e2.Mngr_LName ) AS AD ,
( e1.Mngr_FName + ' ' + e1.Mngr_LName ) AS Supervisor ,
( e1.First_Name + ' ' + e1.Last_Name ) AS Case_Planner
FROM [New_EEs].[dbo].[vw_SBC_Employee_Info] e1
JOIN [New_EEs].[dbo].[vw_ADPFile] e2 ON e1.Supervisor_Position_NBR = e2.POSITION_NBR
JOIN [New_EEs].[dbo].[vw_ADPFile] e3 ON e2.Supervisor_Position_NBR = e3.POSITION_NBR
LEFT JOIN [New_EEs].[dbo].[vw_ADPFile] e4 ON e3.Supervisor_Position_NBR = e4.POSITION_NBR
LEFT JOIN [New_EEs].[dbo].[vw_ADPFile] e5 ON e4.Supervisor_Position_NBR = e5.POSITION_NBR
LEFT JOIN [New_EEs].[dbo].[vw_ADPFile] e6 ON e5.Supervisor_Position_NBR = e6.POSITION_NBR
--ORDER BY e1.UNIT;
) AS A
LEFT JOIN

(
--Query 2

SELECT 'Skill Rating' AS Assessment_Type ,
bs.File_NBR AS ID ,
bs.Skill_NBR ,
bs.Best_Score AS Score ,
bs.Assesment_RND
FROM [New_EEs].[dbo].[SBC_Best_Scores] bs
UNION
SELECT sd.Assessment_Type ,
sd.EXAMINEE_NBR AS ID ,
Skill_NBR ,
sd.SCORE ,
sd.SBC_RND
FROM [New_EEs].[dbo].[vw_SBC_Score_Details] sd
--ORDER BY ID ,
-- Assessment_Type;
) AS B
ON A.File_NBR = B.ID;
Go to Top of Page

mgarret
Starting Member

14 Posts

Posted - 2013-06-10 : 09:52:55
Works perfect! Thanks!
Go to Top of Page
   

- Advertisement -