經常有朋友問行列轉換的問題,
這裡舉一個例子:
帖子地址:http://topic.csdn.Net/u/20070919/10/35d7be36-9992-4a99-a86b-a3269bb50160.Html
主要參考了jdsnhan的寫法,但是jdsnhan的寫法是錯誤的。下面的列子經過嚴格測試。
CREATE TABLE test1
(
ID VARCHAR2(10),
NAME VARCHAR2(100)
);
TRUNCATE TABLE test1;
INSERT INTO test1
VALUES (''12'', ''暗暗,得到'');
INSERT INTO test1
VALUES (''12'', ''大家'');
INSERT INTO test1
VALUES (''12'', ''認可'');
INSERT INTO test1
VALUES (''13'', ''認可'');
INSERT INTO test1
VALUES (''13'', ''很好'');
INSERT INTO test1
VALUES (''bianliang'', ''bianliang'');
INSERT INTO test1
VALUES (''bianliang'', ''bianliang2'');
SELECT *
FROM test1;
SELECT ID, MAX (SUBSTR ((SYS_CONNECT_BY_PATH (NAME, '', '')), 2)) cola
FROM (SELECT ID, NAME, ROWNUM rnum,
ROW_NUMBER () OVER (PARTITION BY ID ORDER BY ID) rn1
FROM test1)
START WITH rn1 = 1
CONNECT BY rnum - 1 = PRIOR rnum and id = prior id
GROUP BY ID;