Solved

Join double column in a table, urgent

Posted on 2004-09-11
7
260 Views
Last Modified: 2012-08-13
Hi everybody,

I have two tables,
         Rank
                RankID
                Name
         InDelegation
                InDelegationName
                LeaderRank
                InvitedRank
Primarykey of Rank Table is RankID, and Rank table has Name column
The Delegation Table has two FK is LeaderRank and InvitedRank.

So how can I get the following Row

InDelegationID, InDelegationName LeaderRankName, InviteRankName

Please, help me. Thanks for any help.
0
Comment
Question by:software22
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:davee_jay
ID: 12033622
Below are two ways to do this one using derived tables the other using correlated sub-queries:

--Derived Tables
SELECT d.InDelegationID, d.InDelegationName, l.Name [LeaderRankName], i.Name [InvitedRankName]
FROM InDelegation d
LEFT OUTER JOIN (
      SELECT RankID, Name
      FROM Rank
) l ON l.RankID = d.LeaderRank
LEFT OUTER JOIN (
      SELECT RankID, Name
      FROM Rank
) i ON i.RankID = d.InvitedRank

--Correlated Subqueries (or nested selects)

SELECT d.InDelegationID, d.InDelegationName,
(--Top 1 used to ensure we only get a single row back,
-- otherwise this will throw an error
      SELECT TOP 1 Name      
      FROM Rank
      WHERE RankID = d.LeaderRank
) [LeaderRankName],
(--Top 1 used to ensure we only get a single row back,
-- otherwise this will throw an error
      SELECT TOP 1 Name
      FROM Rank
      WHERE RankID = d.InvitedRank
) [InvitedrankName]
FROM InDelegation d

Which you decide to use will depend on performance and size of the Rank table.  If in doubt a quick consultation with BOL will reveal the pros and cons of each solution.

Let me know how you get on

DJ
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12033639
you don't need anything as complicated as nested queries, or derived tables - a straightforward join will do the trick :

select  d.InDelagationID, d.InDelegationName, ldr.name AS LeaderRankName, inv.name AS InvitedRankName
from InDelegation
join rank ldr on LeaderRank = ldr.RankID
join rank inv on InvitedRank = inv.RankID
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 500 total points
ID: 12033647
of course that should have read :
select  d.InDelegationID, d.InDelegationName, ldr.name AS LeaderRankName, inv.name AS InvitedRankName
from InDelegation  d
join rank ldr on LeaderRank = ldr.RankID
join rank inv on InvitedRank = inv.RankID
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:davee_jay
ID: 12033667
Thanks for simplifying things BillAn1!

With the correlated sub-query solution I am thinking that we couldn't be sure about how many rows there were in the Rank table for a given InDelegation record (matching on either f_key) and therefore might actually return more than one row for a given InDelegation record - not, I believe what was required.  

I guess this is down to interpretation of the brief at the end of the day!

0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12033713
They are foreign keys, by definition they will link to only 1 record in the Rank table (and RankID is the primary key)- so you don't need to worry about multiple rows.
0
 

Author Comment

by:software22
ID: 12034248
Thanks BillAn1 for your help. I tried and it works :D.
I will ask another question tomorrow. Bye.
0
 

Author Comment

by:software22
ID: 12034320
@davee_jay: I think your Correlated Subqueries is correct, it retuned same result but fk will only return 1 row so you do not need to use top 1. Sorry for my bad English. :D

Thanks.

PS: I have a another question at below before but no more help provided. Please hlp me in urgent !
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21122470.html

Let me know if I have to describe more for that question.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now