I want to create a query to concatenate column values from multiple rows based in an ID column.
This is the structure of my table:
ID Level
000 C
000 FK
111 F
222 FN
222 C
333 F
333 C
444 C
The expected result should be like this:
ID Level
000 C - FK
111 F
222 FN - C
333 F - C
444 C
How can I do this in Oracle SQL Developer?
>Solution :
Is level really your column name? Anyway, use listagg:
Sample data:
SQL> with test (id, c_level) as
2 (select '000', 'C' from dual union all
3 select '000', 'FK' from dual union all
4 select '111', 'F' from dual union all
5 select '222', 'FN' from dual union all
6 select '222', 'C' from dual union all
7 select '333', 'F' from dual union all
8 select '333', 'C' from dual union all
9 select '444', 'C' from dual
10 )
Query:
11 select id,
12 listagg(c_level, ' - ') within group (order by null) result
13 from test
14 group by id
15 order by id;
ID RESULT
--- ----------
000 C - FK
111 F
222 FN - C
333 F - C
444 C
SQL>