Monday, July 21, 2008

How to SELECT Columns as Rows Using SQL

CREATE TABLE TEST(ID NUMBER, A VARCHAR2(10), B VARCHAR2(10), C VARCHAR2(10));
INSERT INTO TEST VALUES(1,'a1','b1','c1');
INSERT INTO TEST VALUES(2,'a2','b2','c2');
INSERT INTO TEST VALUES(3,'a3','b3','c3');
INSERT INTO TEST VALUES(4,'a4','b4','c4');
commit;

SELECT * FROM (
SELECT ID,A FROM TEST
UNION
SELECT ID,B FROM TEST
UNION
SELECT ID,C FROM TEST );

ID A
---------- ----------
1 a1
1 b1
1 c1
2 a2
2 b2
2 c2
3 a3
3 b3
3 c3
4 a4
4 b4
4 c4

No comments:

search engine

Custom Search