SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query to join 3 tables.


Query to join 3 tables.

Author
Message
nehach1234
nehach1234
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
Hi,
I have 2 tables:

Element(Element_Id, Element_Name) and Parent_Child(Parent_Id, Child_Id). Parent_Id and Child Id are actually the element Ids only. An element can have more than 1 parent or more than one child.

Now i want the output like this:

Element_Id Element_Name Parent_Name Child_Name

Plz help !!
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26674 Visits: 17557
Hi and welcome to the forums. In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Tom Thomson
Tom Thomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14610 Visits: 12238
As far as I can tell, what you are asking for is a simple 5-way join: of course in T-SQL that has to written as 4 nested two-way joins - two bottom level inner joins to get names of parent and child of a given element ID, then join one of those to the element table and that result to the Element table again.
To make it easier to read and understand it's probably better to put the two bottom level joins in CTEs and use the old notation for the three way join at the outer level - commas and where clause instead of join operators and on clauses - instead of writing the three way join as two two-way joins.

That gives the following code:
with EP as (select C.Child_Id, E1.Element_Name as Parent_Name 
from Element E1 inner join Parent_Child C on C.Parent_Id = E1.Element_id),
EC as (select P.Parent_Id, E2.Element_Name as Child_Name
from Element E2 inner join Parent_Child P on P.Child_Id = E2.Element_Id)
select E.Element_Name, EP.Parent_Name, EC.Child_Name
from Element E, EP, EC
where EP.Child_Id = E.Element_Id and EC.Parent_Id = E.Element_Id;


I guessed at table defintions and invented some data and tested it; but as Sean said, you should really provide DML and test data for a query like this, as otherwise you may find someone has guessed wrong.

Tom

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search