Skip to content
Advertisement

Column must appear in group by or aggregate function in nested query

I have the following table.

I am trying to query the following:

For each year that appears in the Fights table, find the city that held the most fights. For example, if in year 1992, Jersey held more fights than any other city did, you should print out (1992, Jersey)

Here’s what I have so far but I keep getting the following error. I am not sure how I should construct my group by functions.

ERROR: column, ‘ans.fight_round’ must appear in the GROUP BY clause or be used in an aggregate function. Line 3 from (select *

Advertisement

Answer

In Postgres, I would recommend aggregation and distinct on:

This counts how many fights each city had each year, and retains the city with most fight per year.

If you want to allow ties, then use window functions:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement