Search code examples
joinconcatenationleft-joinoracle-sqldeveloper

Oracle SQL Developer - Concatenate more rows into a single row


I'm using Oracle SQL Developer and i have the following select:

select 
    a1.numero_brevetto, a1.titolo, 
    a2.descrizione, a1.data_scadenza_brevetto, 
    a3.organizzazione
from
    b_brevetto a1
full outer join 
    b_tipo_brevetto a2 on a1.tipo_brevetto = a2.tipo_brevetto
left join 
    b_brevetto_titolari a3 on a1.codice_brevetto = a3.codice_brevetto
where 
    a3.corrente = 1 
    and a1.numero_brevetto = 'EP1523489'
order by 
    a1.numero_brevetto 

The output I get is:

NUMERO_BREVETTO        TITOLO                                   DESCRIZIONE                    DATA_SCAD       ORGANIZZAZIONE                                                                                                                                                                                          
---------------------- ---------------------------------------- ------------------------------ --------------- ------------------------------------------------------------------------------------
EP1522312              Titolo 1                                 Brevetto europeo               22-GIU-23       ORG1                                                                                                                                                     
EP1522312              Titolo 1                                 Brevetto europeo               22-GIU-23       ORG2                                                                                                                                                                            
EP1522312              Titolo 1                                 Brevetto europeo               22-GIU-23       ORG3                                                                                                                                                                     
EP1522312              Titolo 1                                 Brevetto europeo               22-GIU-23       ORG4                                                                                                                                                                             

You can see all the columns contain equal values except the last column "ORGANIZZAZIONE", that contains 4 different values.

Starting from the previous select, how can i get the following output?

NUMERO_BREVETTO        TITOLO                                   DESCRIZIONE                    DATA_SCAD       ORGANIZZAZIONE                                                                                                                                                                                          
---------------------- ---------------------------------------- ------------------------------ --------------- ------------------------------------------------------------------------------------
EP1522312              Titolo 1                                 Brevetto europeo               22-GIU-23       ORG1, ORG2, ORG3, ORG4

In other words, how can I get only one record with the last column that contains all the four previous values concatenated and separated from commas?


Solution

  • What you may need is to aggregate with a GROUP BY clause and then get the concatenation of the column with different values, by a LISTAGG:

    SELECT a1.numero_brevetto,
             a1.titolo,
             a2.descrizione,
             a1.data_scadenza_brevetto,
             listagg(a3.organizzazione, ', ') within group (order by a3.organizzazione) 
        FROM b_brevetto a1
             FULL OUTER JOIN b_tipo_brevetto a2
                ON a1.tipo_brevetto = a2.tipo_brevetto
             LEFT JOIN b_brevetto_titolari a3
                ON a1.codice_brevetto = a3.codice_brevetto
       WHERE a3.corrente = 1 AND a1.numero_brevetto = 'EP1523489'
    GROUP by a1.numero_brevetto,
             a1.titolo,
             a2.descrizione,
             a1.data_scadenza_brevetto   
    ORDER BY a1.numero_brevetto