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 (5) by Kawish_Siddiqui

$
0
0

Here is your solution.

I am placing full code so other gurus can correct it if one finds error or better solution.

 

CREATE TABLE myDB.SrcTgtMap
(
	TgtCol VARCHAR(100),
	SrcCol VARCHAR(100),
	Lvl INTEGER
)
PRIMARY INDEX (lvl);

INSERT INTO myDB.SRCTGTMAP VALUES('L3_Tab1.Col1','L2_Tab1.Col3',0);
INSERT INTO myDB.SRCTGTMAP VALUES('L3_Tab1.Col2','L2_Tab2.Col2',0);
INSERT INTO myDB.SRCTGTMAP VALUES('L3_Tab1.Col3','L2_Tab2.Col3',0);
INSERT INTO myDB.SRCTGTMAP VALUES('L3_Tab1.Col4','L2_Tab2.Col1',0);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab1.Col3','L1_Tab3.Col1',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab1.Col3','L1_Tab1.Col1',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab2.Col1','L1_Tab2.Col1',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab2.Col2','L1_Tab2.Col2',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab2.Col2','L1_Tab1.Col2',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab2.Col2','L1_Tab3.Col2',1);
INSERT INTO myDB.SRCTGTMAP VALUES('L2_Tab2.Col3','L1_Tab3.Col3',1);

SELECT a.TgtCol, a.SrcCol, b.SrcCol As Lvl_1
FROM myDB.SRCTGTMAP a 
INNER JOIN myDB.SRCTGTMAP b
ON a.SrcCol = b.TgtCol
AND a.lvl+1 = b.lvl
ORDER BY 1

 

 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



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