Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can some one help with this Sql

Posted on 2011-03-07
2
Medium Priority
?
194 Views
Last Modified: 2012-05-11
I have a table 'a' like this

parent     child  

9          10
9         11       
                                             
I have a table 'b' like this

ID       primary_id       primary_name          type  
 
1            9            cal              big      
1            9            cal              small            
2            9            cal              big      
2            9            cal              small      
4            10            Nev            small                        
4            10            Nev            big
3               16            Ill                big      
I want the output like this ( For the primary_id in table 'b' I want the parent of and child of to be populated based
on table 'a' in the out put .

ID       primary_id       primary_name          type          parent of         child of
 
1            9            cal                               big               10                  null
1            9            cal                            small            10                  null
1            9            cal                              big                       11                  null
1            9            cal                           small                     11                  null
2            9            cal                           big                       10                  null
2            9            cal                           small                     10                  null
2            9            cal                            big                      11                  null
2            9            cal                               small            11                  null
4            10            Nev                             small            null                  9            
4            10            Nev                                big           null                  9
3               16            Ill                                   big            null                  null



Thanks in advance
0
Comment
Question by:vijay11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 35063234
try this.
SELECT t1.*, 
       t2.child  [parent of], 
       t3.parent [child of] 
  FROM b t1 
       LEFT JOIN a t2 
         ON t1.primary_id = t2.parent 
       LEFT JOIN a t3 
         ON t1.primary_id = t3.child

Open in new window

0
 

Author Comment

by:vijay11
ID: 35069629
Here In the out put the number of rows  with  primary_id     9  gets double , because primary_id     9 has 2 childs in the table 'a'  ,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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