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
  • 104,344
  • 11
  • 159
  • 196
Omri
  • 1,456
  • 4
  • 26
  • 51

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
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Barry
  • 3,555
  • 1
  • 16
  • 25
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,544
  • 13
  • 21