Solved

Can some one help with this Sql

Posted on 2011-03-07
2
188 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
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

816 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

12 Experts available now in Live!

Get 1:1 Help Now