listduckdb

duckdb - aggregate string with a given separator


The standard aggregator makes coma separated list:

$ SELECT list_string_agg([1, 2, 'sdsd'])
'1,2,sdsd'

How can I make a smicolumn separated list or '/'-separated? Like '1;2;sdsd' or '1/2/sdsd'.


Solution

  • DuckDB now proposes the following duckdb documentation page :

    select array_to_string(['1', '2', 'sdsd'], '/')
    

    which is a more compact way to write:

    SELECT list_aggr(['1', '2', 'sdsd'], 'string_agg', '/')
    

    nb: it only works if all elements are of the same type