行转列

WITH tab AS
(           
          SELECT 1 as col1, 2 as col2, 'Smith' as created_by FROM dual
UNION ALL SELECT 1 as col1, 2 as col2, 'John'  as created_by FROM dual
UNION ALL SELECT 1 as col1, 3 as col2, 'Ajay'  as created_by FROM dual
UNION ALL SELECT 1 as col1, 4 as col2, 'Ram'   as created_by FROM dual
UNION ALL SELECT 1 as col1, 5 as col2, 'Jack'  as created_by FROM dual
)
SELECT listagg(col2_with_nulls, ',') within
 GROUP(
 ORDER BY col2_with_nulls) col2_list
  FROM (SELECT col1,
               CASE
                 WHEN lag(col2) over(ORDER BY col2) = col2 THEN
                  NULL
                 ELSE
                  col2
               END AS col2_with_nulls,
               created_by
          FROM tab);
请使用浏览器的分享功能分享到微信等