Solved

Can some one help with this Sql

Posted on 2011-03-07
2
186 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

15 Experts available now in Live!

Get 1:1 Help Now