I have a table with id
, year
and count
.
I want to get the MAX(count)
for each id
and keep the year
when it happens, so I make this query:
SELECT id, year, MAX(count)
FROM table
GROUP BY id;
Unfortunately, it gives me an error:
ERROR: column "table.year" must appear in the GROUP BY clause or be used in an aggregate function
So I try:
SELECT id, year, MAX(count)
FROM table
GROUP BY id, year;
But then, it doesn't do MAX(count)
, it just shows the table as it is. I suppose because when grouping by year
and id
, it gets the max for the id
of that specific year.
So, how can I write that query? I want to get the id
´s MAX(count)
and the year when that happens.