For the documentation :
If DISTINCT is specified in addition to order_by_clause, then all ORDER BY expressions must match the regular arguments of the aggregate; that is, you cannot sort an expression that is not included in the DISTINCT list.
So try
select string_agg(distinct a || '-' || b, ',' order by a || '-' || b)
from a_table;
or use distinctin a view:
select string_agg(a || '-' || b , ',' order by a, b)
from (
select distinct a, b
from a_table
) s;
source
share