1

I have a table that contains duplicate values in the columns c1, c2, c3, c4, c5.
I want to list all the dates in the row that contain the values generated the group by function.

datum          c1    c2    c3    c4    c5    number
2001-04-22     1     2     3     4     5     2
1999-08-24     2     3     4     5     6     2
2005-11-08     2     4     5     6     7     2
1998-03-20     1     2     3     4     5     2
2009-07-02     2     3     4     5     6     2
1996-05-21     2     4     5     6     7     2

Result should look like this:

datum                      c1    c2    c3    c4    c5    number
1998-03-20 , 2001-04-22    1     2     3     4     5     2
1999-08-24 , 2009-07-02    2     3     4     5     6     2
1996-05-21 , 2005-11 08    2     4     5     6     7     2

in the source table can be duplicate values ​​in all columns, what will be the solution?

2 Answers 2

5
select string_agg(to_char(datum, 'yyyy-mm-dd'), ','),
       c1, 
       c2,
       c3, 
       c4, 
       c5, 
       number
from some_table
group by c1, c2,c3, c4, c5, number
Sign up to request clarification or add additional context in comments.

Comments

1

If you are satisfied with an array of date in the result you can use the simple and fast array_agg().

To get the array in a specific order add ORDER BY in the aggregate function. The manual:

This ordering is unspecified by default, but can be controlled by writing an ORDER BY clause within the aggregate call [...]

SELECT array_agg(datum ORDER BY datum) AS datum_arr
     , c1, c2, c3, c4, c5
     , min(number) AS number  -- you did not specify how to aggregate number
FROM   tbl
GROUP  BY c1, c2,c3, c4, c5;

For simple queries, it's typically faster to order in a subquery. See:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.