Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

SQL to transpose (row to column) any level (nth level) of data - response (6) by Koushik Chandra

$
0
0

Thanks for your reply, but when I will have a dataset like below :

    Tgt		    Src		lvl
============    ============    ====
L4_Tab1.Col1	L3_Tab2.Col3	0
L4_Tab1.Col4	L3_Tab2.Col1	0
L4_Tab1.Col2	L3_Tab1.Col2	0
L4_Tab1.Col3	L3_Tab1.Col3	0
L3_Tab2.Col1	L2_Tab2.Col3	1
L3_Tab1.Col2	L2_Tab2.Col2	1
L3_Tab2.Col3	L2_Tab1.Col3	1
L3_Tab1.Col3	L2_Tab2.Col3	1
L2_Tab2.Col3	L1_Tab3.Col3	2
L2_Tab1.Col3	L1_Tab3.Col1	2
L2_Tab2.Col2	L1_Tab2.Col2	2
L2_Tab1.Col3	L1_Tab1.Col1	2
L2_Tab2.Col2	L1_Tab1.Col2	2
L2_Tab2.Col2	L1_Tab3.Col2	2

I have to write a query like below :

SELECT a.Tgt, a.Src, b.Src AS Lvl_1, c.Src AS Lvl_2
FROM SRCTGTMAP a 
INNER JOIN SRCTGTMAP b
ON a.Src = b.Tgt
AND a.lvl+1 = b.lvl
INNER JOIN SRCTGTMAP C
ON b.Src = c.Tgt
AND b.lvl+1 = c.lvl
ORDER BY 1

Means I have to use the same table again in inner join to get another level.
Can this be done with a query which can work in any (multiple) level?
Regards,
Koushik


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>