Solved

Join double column in a table, urgent

Posted on 2004-09-11
7
295 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

756 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