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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Join On Two Columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pyu.agrawal
Starting Member

29 Posts

Posted - 04/03/2009 :  09:07:44  Show Profile  Reply with Quote
I have got tables.
Table 1: Parties
PartyID | PartyName | Email


Table 2: Inwards
Inward ID | IDate | FromID | ToID | ....


My requirement is that I need a list that would display the output as
InwardID | IDate | FromParty | ToParty | ...


How do I use get the PartyName from PartyName, in the two columns namely FromParty, and ToParty

Using Inner Join would fill in either of FromParty, or ToParty.

Thanks
Peeyush Agrawal

pyu.agrawal
Starting Member

29 Posts

Posted - 04/03/2009 :  09:14:43  Show Profile  Reply with Quote
I have got tables.
Table 1: Parties

PartyID | PartyName | Email


Table 2: Inwards

Inward ID | IDate | FromID | ToID | ....


My requirement is that I need a list that would display the output as

InwardID | IDate | FromParty | ToParty | ...


How do I use get the PartyName from Parties, in the two columns namely FromParty, and ToParty

Using Inner Join would fill in either of FromParty, or ToParty.

Thanks
Peeyush Agrawal
Go to Top of Page

Wozer
Starting Member

29 Posts

Posted - 04/03/2009 :  09:17:57  Show Profile  Reply with Quote
Do you have a Common field in both tables that you could inner join on? For example if you had a column for PartyID in the Inwards Table, you could do something like the following

SELECT InwardID, IDate, FromParty, ToParty --Whatever else you need
FROM Inwards
INNER JOIN Parties ON Inwards.PartyID = Parties.PartyID

If you don't have a common Column on each table, you cant join them.
Go to Top of Page

Ifor
Aged Yak Warrior

615 Posts

Posted - 04/03/2009 :  09:55:27  Show Profile  Reply with Quote
SELECT I.InwardID, I.IDate, PF.PartyName AS FromParty, PT.PartyName AS ToParty
FROM Inwards I
    JOIN Parties PF
        ON I.FromID = PF.PartyID
    JOIN Parties PT
        ON I.ToID = PT.PartyID

Go to Top of Page

maeenul
Starting Member

20 Posts

Posted - 04/04/2009 :  05:42:35  Show Profile  Reply with Quote
yes, Ifor's post is the way of doing this. If you have two columns that is the referring to one column in a table, then you have to join twice.



-----------------------
maeenul
http://www.programmingsolution.net
http://sqlservertipsntricks.blogspot.com
Go to Top of Page

pyu.agrawal
Starting Member

29 Posts

Posted - 04/14/2009 :  12:49:03  Show Profile  Reply with Quote
Thanks iFor,
Your script worked :)
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.09 seconds. Powered By: Snitz Forums 2000