SQL Query to display levels

Krish 81 Reputation points
2022-07-08T20:52:27.323+00:00

Dear All,

Can some one suggest me to prepare a query for the below sample please ?

Input :
Column1 ,column 2
1,4
1,138
138,2
138,3

Expected output from a query :
Column1,column2,column 3
1,4,NULL
1,138,2
1,138,3

Its like Dividing two columns into 3 based on the numbers on Level values.

Please consider this is like family tree.. grandfather, Father and son data .. but based on two columns like column1 and column2.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.9K Reputation points
    2022-07-08T20:58:21.453+00:00

    Check a query:

    select t1.Column1, t1.Column2, t2.Column2  
    from MyTable t1  
    left join MyTable t0 on t0.Column2 = t1.Column1  
    left join MyTable t2 on t2.Column1 = t1.Column2  
    where t0.Column2 is null  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.