SELECT string_agg( distinct a || '-' || b,',' ORDER BY a,b) FROM table;
ERROR: in an aggregate with disTINCT,ORDER BY expressions must appear in argument list
解决方法
对于
the documentation:
If disTINCT is specified in addition to an order_by_clause,then all the ORDER BY expressions must match regular arguments of the aggregate; that is,you cannot sort on an expression that is not included in the disTINCT list.
所以试试吧
select string_agg(distinct a || '-' || b,' order by a || '-' || b) from a_table;
或在派生表中使用distinct:
select string_agg(a || '-' || b,' order by a,b) from ( select distinct a,b from a_table ) s;