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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Join On Two Columns

Author  Topic 

pyu.agrawal
Starting Member

29 Posts

Posted - 2009-04-03 : 09:07:44
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 - 2009-04-03 : 09:14:43
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 - 2009-04-03 : 09:17:57
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

700 Posts

Posted - 2009-04-03 : 09:55:27
[code]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[/code]
Go to Top of Page

maeenul
Starting Member

20 Posts

Posted - 2009-04-04 : 05:42:35
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 - 2009-04-14 : 12:49:03
Thanks iFor,
Your script worked :)
Go to Top of Page
   

- Advertisement -