1

I have a query shown below

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

FROM league 
GROUP BY league.country

How can I sort by league.name::varchar in array_agg function?

1

1 Answer 1

13

Aggregates accept an ORDER BY. So you can write:

array_agg(thecol ORDER BY someothercol)

e.g.

array_agg(
  DISTINCT 
  row(league.id, league.name::varchar) 
  ORDER BY league.name
) AS league_names, 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.