I have a table called votes
vid stateid voterid voteeid 1 1 1 1 2 1 2 1 3 1 3 1 4 1 4 1 5 1 5 2 6 1 6 2 7 2 7 3 8 2 8 3 9 2 9 4
I want to find the maximum number of votes to a person per state.
Select Top 1 voteeid from votes where stateid = 1 group by voteeid order by count(voteeid) desc
This query is capable of finding maximum number of votes to a person in a single state.
I want to find maximum number votes to a person in every state. In short I want a output like this
votes voteeid 4 1 2 3
Can some one point me in a right direction ?
Thanks
You can
group byto find the number of votes per state, and userankto select all top votee’s:Use
row_numberinstead ofrankif you don’t want ties.For MySQL, you can’t use ranking functions, so you have to add an additional query to find the maximum vote per state:
Example on SO Data