I need to find the most popular name per year from the below data based on the combined total count for a name each year. Note there can be multiple entries per year (as seen below).
ID person_name total_count person_year
1 MIKE 1 2006
2 MIKE 2 2007
3 MIKE 4 2007
4 MIKE 3 2008
5 TED 1 2006
6 TED 2 2007
7 TED 4 2008
8 TED 7 2008
9 MOOKIE 1 2006
10 MOOKIE 12 2006
11 MOOKIE 5 2007
12 MOOKIE 3 2008
The SQL I need to write would produce the below result:
person_name max_value person_year
MOOKIE 13 2006
MIKE 6 2007
TED 11 2008
Creating the SUM table is easy:
SELECT id, person_name,SUM(total_count) AS sum_count, person_year FROM temp_table GROUP BY person_name, person_year;
This gives me the Sum count per year for each name.
The problem is any MAX logic I write doesn't carry the associated NAME with the selected MAX when I group by YEAR. I've tried numerous variations and none of them work. I would have thought the below would work, but the NAME is mismatched:
SELECT id, person_name, MAX(sum_count) AS max_count, person_year FROM
(SELECT id, person_name, SUM(total_count) AS sum_count, person_year FROM temp_table GROUP BY person_name, person_year) AS PC
GROUP BY person_year;
It returns:
1 MIKE 13 2006
2 MIKE 6 2007
4 MIKE 11 2008
So I don't know how to map the selected MAX grouped by YEAR to the proper name... That's the only piece I'm missing.
Any help on this would be appreciated.