-1

I have a table that looks like this:

+---------------------------+
| col1   | col2    | col3   |
+--------+---------+--------+
| 1      | A       | apple  |
| 1      | A       | banana |
| 2      | B       | grapes |
| 2      | A       | orange |
+--------+---------+--------+

What I wanted is to combine all in col3 that has the same col1 and col2

The output that I wanted is like this:

+----------------------------------+
| col1   | col2    | col3          |
+--------+---------+---------------+
| 1      | A       | apple, banana |
| 2      | B       | grapes        |
| 2      | A       | orange        |
+--------+---------+---------------+
Miracle
  • 387
  • 5
  • 31

1 Answers1

1

You could use this:

SELECT col1, col2, 
    LISTAGG(col3, ', ') WITHIN GROUP (ORDER BY col1, col2) AS col3
FROM table_name
GROUP BY col1, col2
ORDER BY col1, col2;
Pham X. Bach
  • 5,284
  • 4
  • 28
  • 42