oracle行转列拼接

用法:
LISTAGG('待转的列','分割符号') WITHIN GROUP(ORDER BY 列字段) AS XXX 字段 FROM 表 WHERE 条件 group by 分组条件 ;

WITH TEMP AS
 (SELECT 'CHINA' NATION, 'GUANGZHOU' CITY
    FROM DUAL
  UNION ALL
  SELECT 'CHINA' NATION, 'SHANGHAI' CITY
    FROM DUAL
  UNION ALL
  SELECT 'CHINA' NATION, 'BEIJING' CITY
    FROM DUAL
  UNION ALL
  SELECT 'USA' NATION, 'NEW YORK' CITY
    FROM DUAL
  UNION ALL
  SELECT 'USA' NATION, 'BOSTOM' CITY
    FROM DUAL
  UNION ALL
  SELECT 'JAPAN' NATION, 'TOKYO' CITY
    FROM DUAL)
SELECT NATION, LISTAGG(CITY, ',') WITHIN GROUP(ORDER BY CITY) AS CITIES
  FROM TEMP
 GROUP BY NATION

 

posted @ 2021-06-15 21:42  阳光下的行者  阅读(322)  评论(0编辑  收藏  举报