在array_agg函数中排序

时间:2014-03-09 01:33:31

标签: sql postgresql-9.1

我的查询如下所示

SELECT 
  league.country as id, 
  array_agg(DISTINCT row(league.id, league.name::varchar)) AS league_names, 

FROM league 
GROUP BY league.country

如何按照array_agg函数中的league.name::varchar排序?

1 个答案:

答案 0 :(得分:3)

汇总接受ORDER BY。所以你可以写:

array_agg(thecol ORDER BY someothercol)

e.g。

array_agg(
  DISTINCT 
  row(league.id, league.name::varchar) 
  ORDER BY league.name
) AS league_names,