Search code examples
amazon-redshiftstring-aggregation

How to concatenate varchar into a string in Redshift table


I have a table as follows:

create table #t1(a1 varchar(2))

insert into #t1 select 'AA'
insert into #t1 select 'BB'

I want final result as follows: Which aggregation function should I use.

'AA,BB'

Solution

  • From LISTAGG Function - Amazon Redshift:

    select listagg(sellerid, ', ') within group (order by sellerid) from sales
    where eventid = 4337;
    
    listagg                                                                                                                                 
    ----------------------------------------------------
    380, 380, 1178, 1178, 1178, 2731, 8117, 12905, 32043