How to retrieve two columns data in A,B format in Oracle

Tim Hall has a pretty canonical list of string aggregation techniques in Oracle.

Which technique you use depends on a number of factors including the version of Oracle and whether you are looking for a purely SQL solution. If you are using Oracle 11.2, I’d probably suggest using LISTAGG

SELECT column1, listagg( column2, ',' ) WITHIN GROUP( order by column2 )
  FROM table_name
 GROUP BY column1

If you are using an earlier version of Oracle, assuming you don’t need a purely SQL solution, I would generally prefer using the user-defined aggregate function approach.

Leave a Comment