SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining of two statements
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

OKIDOKI
Starting Member

3 Posts

Posted - 06/25/2014 :  16:40:18  Show Profile  Reply with Quote
Hi

I am having difficulty with joining these two statements and I just can't seem to get a way. I hope one of you's can be of assistance.

Here is the query:

select Name,Surname,position
from dbo.Employee, dbo.Stafflink
where dbo.Employee.ID = dbo.Stafflink.empID
group by Name,Surname,Position

select Employee.Name as ManagerName, Employee.Position as ManagerPosition
FROM dbo.Employee, dbo.Stafflink
WHERE dbo.Employee.ID = dbo.Stafflink.ManID

How can I join both of these statements??? Please help as I am still new to SQL

tkizer
Almighty SQL Goddess

USA
37316 Posts

Posted - 06/25/2014 :  16:47:47  Show Profile  Visit tkizer's Homepage  Reply with Quote
Why are you using a GROUP BY in the first query?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 06/25/2014 :  17:02:14  Show Profile  Reply with Quote
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

OKIDOKI
Starting Member

3 Posts

Posted - 06/25/2014 :  18:35:23  Show Profile  Reply with Quote
@tkizer
Sorry I have been trying so many things and I accidently left that in statement, please ignore it

@Lamprey
Thank you for the links, They have certainly assisted... Unfortunately I need to use a join and not a union, I wish I could use the union for this question
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37316 Posts

Posted - 06/25/2014 :  18:46:19  Show Profile  Visit tkizer's Homepage  Reply with Quote
OKIDOKI, those links are to show YOU how to post your question so that WE can help YOU. Please reread the links.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Ifor
Aged Yak Warrior

619 Posts

Posted - 06/26/2014 :  07:47:15  Show Profile  Reply with Quote
Try:

SELECT E.Name AS EmpName
	,E.Surname AS EmpSurname
	,E.Position AS EmpPosition
	,COALESCE(M.Name, 'None') AS ManagerName
	,COALESCE(M.Position, 'None') AS ManagerPosition
FROM dbo.Stafflink L
	JOIN dbo.Employee M
		ON L.ManID = M.ID
	RIGHT JOIN dbo.Employee E
		ON L.empID = E.ID;

Go to Top of Page

OKIDOKI
Starting Member

3 Posts

Posted - 06/26/2014 :  09:58:35  Show Profile  Reply with Quote
Thank you all for your assistance, I finally got it...

select Employee.Name,Employee.Surname,Employee.Position,Manager.name as managerName,manager.Position as managerposition
from(( Employee
inner join Stafflink
on Employee.ID=Stafflink.empID)
inner join Employee manager
on manager.ID=Stafflink.ManID)
order by Employee.id


Ifor I also tried yours and it worked beautifully, thank you.... I must learn more about the COALESCE as it is the first I have heard of it or seen in... Thank you all...
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000