Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can some one help with this Sql

Posted on 2011-03-07
2
Medium Priority
?
196 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 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

885 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