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

oracle, string

Solution

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.

Problem

I have two columns in oracle database ``` +---------+---------+ | Column1 | Column2 | +---------+---------+ | A | 1 | | A | 2 | +---------+---------+ ``` I want to retireive the data like i will get data as result ``` +---------+---------+ | Column1 | Column2 | +---------+---------+ | A | 1,2 | +---------+---------+ ``` Please provide me the solution.

Original source