0

I have created a table emp having two attributes id and item. Id has duplicate values containing records for item.

Initial table :

id          item
1           A
1           B
1           C
2           D
2           E
2           F   

I want to get a output as follows by writing a query in SQL . Database used is oracle.

id          item
1           A,B,C
2           D,E,F
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
Divya
  • 1
  • 1

1 Answers1

1

Nowadays, we do it with listagg.

SQL> with your_table (id, item) as
  2    (select 1, 'A' from dual union all
  3     select 1, 'B' from dual union all
  4     select 1, 'C' from dual union all
  5     select 2, 'D' from dual union all
  6     select 2, 'E' from dual
  7    )
  8  select id,
  9         listagg(item, ',') within group (order by item) item
 10  from your_table
 11  group by id;

        ID ITEM
---------- --------------------
         1 A,B,C
         2 D,E

SQL>
Littlefoot
  • 131,892
  • 15
  • 35
  • 57