1

I have a table with the columns "Sequence-ID", "Event-ID", "Value"

To one sequence belongs up to 5 events and each event has one value e.g.:

|Sequence-ID | Event-ID | Value |
|------------|----------|-------|
|1           |         1|      7|
|1           |         2|      2|
|1           |         3|      5|
|1           |         4|      9|
|1           |         5|     12|
|2           |         1|     15|
|2           |         2|      8|
|2           |         3|     10|
|2           |         4|     21|
|2           |         5|     17|

How can I select each sequence into one row, separated with semicolon?

|Sequence-ID |Value        |
|------------|-------------|
|1           |7;2;5;9;12   |
|2           |15;8;10;21;17|
Matthias
  • 461
  • 7
  • 24

4 Answers4

3

Use STRING_AGG(col1)

   select Sequence-ID , STRING_AGG(Value , ';')  AS newValue
    from table
    GROUP  BY Sequence-ID;
dcieslak
  • 2,697
  • 1
  • 12
  • 19
1

Maybe you need to use STRING_AGG function?

http://scn.sap.com/thread/3469848

http://scn.sap.com/thread/3575156

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
0

You can do it with STRING_AGG function, here is SQL query:

SELECT ID, STRING_AGG(Value, ';')  AS Names
FROM TAB
GROUP BY Sequence-ID;
Serge
  • 2,574
  • 18
  • 26
0

You can do like this:

SELECT Sequence-ID, string_agg(Value::text, ';'::text) from table group by Sequence-ID
rabah leghettas
  • 117
  • 1
  • 6