oracle語句中如何把一列的值合併為一個值,用逗號隔開
阿新 • • 發佈:2019-01-28
在Oracle中則可以使用wm_concat 或 ListAgg
01.CREATE TABLE MyTest(xType NUMBER,City nvarchar2(200)); 02./ 03. 04.INSERT INTO MyTest(xType,City) 05.SELECT 1,'北京' FROM dual UNION ALL 06.SELECT 1,'上海' FROM dual UNION ALL 07.SELECT 1,'廣州' FROM dual UNION ALL 08.SELECT 2,'武漢' FROM dual UNION ALL 09.SELECT 2,'杭州' FROM dual UNION ALL 10.SELECT 2,'廈門' FROM dual 11.COMMIT;
使用wm_Concat:
SELECT xType,wmsys.wm_concat(to_char(City)) AS xCity FROM MyTest GROUP BY xType
使用ListAgg:
SELECT xType, ListAgg(to_char(City),',') WITHIN GROUP(ORDER BY xType) AS xCity FROM MyTest GROUP BY xType
XTYPE XCITY
---------- --------------------------------------
1 北京,廣州,上海
2 杭州,武漢,廈門