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

后端 未结 4 1374
刺人心
刺人心 2020-11-27 23:49

I have two columns in oracle database

+---------+---------+
| Column1 | Column2 |
+---------+---------+
| A       | 1       |
| A       | 2       |
+--------         


        
4条回答
  •  情深已故
    2020-11-27 23:54

    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.

提交回复
热议问题