Search code examples
sqloracle-databaseoracle12c

Oracle select distinct merge duplicates


I have a simple table in my database which looks like this

+----------+-------------+
| category | category_id |
+----------+-------------+
| cars     |           1 |
| bikes    |           2 |
| planes   |           3 |
| bikes    |           4 |
| cars     |           5 |
+----------+-------------+

As you can see the data is not the best and there are duplicate categories with different category_ids. My goal is to get a result where each category occurs one time and if there are multiple ids for the same category name merge them (comma-separated or sth. like that). So for the example, the result should look like this

+----------+-------------+
| category | category_id |
+----------+-------------+
| cars     |         1,5 |
| bikes    |         2,4 |
| planes   |           3 |
+----------+-------------+

How can I archive that result. My current query looks like this

select distinct category, category_id 
from my_table
order by 1;

I have absolutely no idea how to archive this kind of merge with a distinct statement. Is the distinct statement the right approach or should I use another one?


Solution

  • You are right; a category ID should represent a category, so such a table should not contain the same category twice (with two different IDs). The table lacks a unique constraint on category.

    You want to see the data represented differently, which you can achieve with aggregation:

    select
      category,
      listagg(category_id, ', ') within group (order by category_id) as category_ids
    from my_table
    group by category
    order by category;