0

I'm using Oracle SQL and i have a question regarding group by command.

I have the following table:

Column_A (int)
Column_B (int)

Example for data in the table:

Column_A | Column_B
 11      |    2
 23      |    3
 32      |    4
 32      |    10
 11      |    23
 23      |    11
 44      |    1
 23      |    5

I want to Group by Column_A while the values of Column_b will be terminated by commas. Output table:

Column_A | Column_B
 11      |  2, 23
 23      |  3, 11
 32      |  4, 10, 5
 44      |  1

Any recommendation how to do that?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Omri
  • 1,436
  • 7
  • 31
  • 61

2 Answers2

2

Use function listagg

SELECT Column_A, listagg( Column_B, ',' ) WITHIN GROUP( order by Column_B)
  FROM table_name
 GROUP BY Column_A
Barry
  • 3,683
  • 1
  • 18
  • 25
  • @a_horse_with_no_name I was sloppy again with the code formatting, thanks for the edit. – Barry Oct 16 '14 at 09:09
2

You can use LISTAGG:

SELECT column_A,
       LISTAGG(column_B, ', ') WITHIN GROUP (ORDER BY column_B) column_B
  FROM your_table
 GROUP
    BY column_A
DirkNM
  • 2,614
  • 15
  • 21