1

I have a table in Oracle where there are two columns. In the first column, sometimes there are duplicate values that corresspond to a different value in the second column. How can I write a query that shows only unique values of the first column and all possible values from the second column?

The table looks somewhat like below

COLUMN_1   |   COLUMN_2

NUMBER_1   |       4
NUMBER_2   |       4 
NUMBER_3   |       1
NUMBER_3   |       6
NUMBER_4   |       3
NUMBER_4   |       4
NUMBER_4   |       5
NUMBER_4   |       6
siddhu
  • 369
  • 1
  • 6
  • 16

1 Answers1

6

You can use listagg() if you are using Oracle 11G or higher like

SELECT 
    COLUMN_1,
    LISTAGG(COLUMN_2, '|') WITHIN GROUP (ORDER BY COLUMN_2) "ListValues"
FROM table1
GROUP BY COLUMN_1

Else, see this link for an alternative for lower versions

Oracle equivalent of MySQL group_concat

Rahul
  • 76,197
  • 13
  • 71
  • 125