Paulo Teixeira
Paulo Teixeira

Reputation: 458

Group query rows result in one result

I need make a query that I get the result and put in one line separated per comma.

For example, I have this query:

SELECT 
   SIGLA
FROM
 LANGUAGES

This query return the result below:

SIGLA

ESP
EN
BRA

I need to get this result in one single line that way:

SIGLA

ESP,EN,BRA

Can anyone help me?

Thank you!

Upvotes: 0

Views: 6854

Answers (3)

MT0
MT0

Reputation: 167842

If you want to get the values grouped together in the order that Oracle produces the rows then:

SELECT LISTAGG( SIGLA, ',' ) WITHIN GROUP ( ORDER BY ROWNUM ) AS SIGLA
FROM   LANGUAGES;

If you want alphabetical ordering then replace ORDER BY ROWNUM with ORDER BY SIGLA (or, curiously, ORDER BY NULL).

Upvotes: 1

Zahiro Mor
Zahiro Mor

Reputation: 1718

try

SELECT LISTAGG( SIGLA,  ',' )   within group (order by SIGLA) as NewSigla FROM LANGUAGES

Upvotes: 1

Taku_
Taku_

Reputation: 1625

SELECT LISTAGG(SIGLA, ', ') WITHIN GROUP (ORDER BY SIGLA) " As "S_List" FROM LANGUAGES

Should be the listagg sequence you are needing

Upvotes: 2

Related Questions