4

For sample data as below,

Col1 Col2
1    A
1    B
1    C
2    A
2    B

the output I am looking for is

COL1 COL2
1    A B C
2    A B

This can be done using LISTAGG on Oracle or recursive queries on other DBs but Redshift doesnt support both.

How do I achieve this on Redshift

3

6 Answers 6

8

They just added LISTAGG() to Redshift (2015-07-31). http://docs.aws.amazon.com/redshift/latest/dg/r_LISTAGG.html

1
  • 2
    Note that this doesn't work on System / Catalog tables. Commented Sep 1, 2017 at 6:58
3

Here is a solution from another similar question -

SELECT col1,
   LISTAGG(col2,', ')
WITHIN GROUP (ORDER BY col2)
OVER (PARTITION BY col1) AS EMPLOYEE
FROM YOUR_TABLE
ORDER BY col1

This question.

Redshift has introduced LISTAGG window function that makes it possible to do so now. Here is a quick solution to your problem - may or may not be useful but putting it here so that people will know!

Here is the documentation about the function. || This is the announcement.

2

Try getting row_number of each group row in a sub query and then on top of the subquery do

max(case when row_num_value =1 then col_value end)||','
max(case when row_num_value =2 then col_value end)||','
max(case when row_num_value =3 then col_value end)||.....

This is ofcourse a limited version with upper limit upto whatever you chose.

3
  • I thought about this approach and yes it works as long as there is a small number of row_num_values. I am looking at ways to not hard code it plus scale it for a large number of values. Thanks Commented Sep 29, 2014 at 6:59
  • Right now there is no such UDF in Redshift and we are waiting for UDF functionality to be opened by Redshift to do these.
    – androboy
    Commented Sep 30, 2014 at 9:17
  • Considering that LISTAGG() in Redshift doesn't work on System / Catalog tables, I had to use this workaround (assuming <10 rows) and still got the work done. Thanks! Commented Sep 2, 2017 at 4:40
0

Try this:

SELECT COL1,STRING_AGG(COL2,' ') AS COL2 FROM TABLE_NAME GROUP BY COL1
1
-1
SELECT Col1, ARRAY_TO_STRING(ARRAY_AGG(Col2 ORDER BY Col2 ASC), ' ')
FROM MyTable
GROUP BY Col1;

I don't know what version of PostgreSQL you are using. Prior to version 8.4, you would have had to define the function array_agg before using it:

CREATE AGGREGATE array_agg (anyelement)
(
    sfunc = array_append,
    stype = anyarray,
    initcond = '{}'
);
1
  • 1
    I am using Amazon Redshift. It works with Postgres driver 8.1 but it doesn't allow creating functions yet. Commented Sep 9, 2014 at 16:01
-1
select
    distinct COL_1,
    listagg(distinct COL_2,
    ',') within group (
    order by COL_2 desc) as my_list
from
    table
group by 1

However I have a following question as to how we can retrieve the 2nd element of this list without using substring ( e.g if its an array we can just do array[1])

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.