0

I have in my table this kind of lines:

key    column1     column2     column3     column4
657    Monsieur    Jean        Valjean     Directeur
657    Monsieur    Jean        Valjean     Président
657    Monsieur    Jean        Valjean     Secrétaire
785    Monsieur    Hervé       Wo          Trésorier
963    Madame      Élodie      Haka        Vendeuse
963    Madame      Élodie      Haka        Responsable

The data is the same for the column key, column1, column2 and column3, but only the data of column4 is different.

I need a request to optain:

key    column1     column2     column3     column4
657    Monsieur    Jean        Valjean     Directeur;Président;Secrétaire
785    Monsieur    Hervé       Wo          Trésorier
963    Madame      Élodie      Haka        Vendeuse;Responsable

Thanks for help.

Macbernie
  • 1,303
  • 6
  • 24
  • 48
  • 1
    Possible duplicate of [Postgresql GROUP\_CONCAT equivalent?](http://stackoverflow.com/questions/2560946/postgresql-group-concat-equivalent) – Vamsi Prabhala May 25 '16 at 23:23

1 Answers1

1

Use group by:

select key, col1, col2, col3, string_agg(col4, ';')
from t
group by key;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • It doesn't work, group by need the col1, col2, col3 columns. But if I add them, there nothing on string_agg column – Macbernie May 25 '16 at 23:31
  • 1
    A quick test suggests that this works with the longer group by: `select key, column1, column2, column3, string_agg(column4, ';') from t group by key, column1, column2, column3;` -- are you perhaps trying to group by column4, too, in your test? – jmelesky May 25 '16 at 23:46