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
 Help with simple query

Author  Topic 

steve48
Starting Member

6 Posts

Posted - 2010-04-09 : 10:59:10
I have a PersonTable with PersonID, FirstName, Last Name, etc

I have an OfferTable with BuyerID, AgentId, SellerID.

I want to write a query to get the names of the Buyer, Agent, Seller from the PersonTable based on the corresponding IDs in the OfferTable.

I have tried a bunch of different things but nothing worked. Any help will be appreciated.

Steven Greenbaum

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-09 : 11:03:58
Like this:
select
Buyer.LastName,
Agent.LastName,
Seller.LastName
from OfferTable o
join PersonTable as Buyer on Buyer.PersonID = o.BuyerID
join PersonTable as Agent on Agent.PersonID = o.AgentID
join PersonTable as Seller on Seller.PersonID = o.SellerID


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-09 : 11:04:34
Try this:

Select B.FirstName as Buyer , A.FirstName as Agent, S.FirstName as Seller
from OfferTAble OT inner join PersonTable B on B.PersonId = OT.BuyerId
inner join PersonTable A on a.PersonId = OT.AgentId
inner join PersonTable s on s.PersonId = OT.SellerID

Regards,
Bohra
Go to Top of Page

steve48
Starting Member

6 Posts

Posted - 2010-04-09 : 12:13:23
Thank you for your quick reply, This worked

Steven Greenbaum
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-09 : 12:54:44


Regards,
I am here to learn from Masters and help new bees in learning.
Go to Top of Page
   

- Advertisement -