Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate column values from multiple rows in Oracle SQL

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading