Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4534646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:18:25+00:00 2026-05-21T14:18:25+00:00

I have a table called votes vid stateid voterid voteeid 1 1 1 1

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:18:25+00:00Added an answer on May 21, 2026 at 2:18 pm

    You can group by to find the number of votes per state, and use rank to select all top votee’s:

    select  stateid
    ,       voteeid
    ,       votes
    from    (
            select  stateid
            ,       voteeid
            ,       count(*) as votes
            ,       rank() over (partition by stateid order by count(*) desc) 
                        as rn
            from    @votes
            group by
                    stateid
            ,       voteeid
            ) cnt
    where   rn = 1
    

    Use row_number instead of rank if 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:

    select  cnt.stateid
    ,       cnt.voteeid
    ,       cnt.votes
    from    (
            select  stateid
            ,       voteeid
            ,       count(*) as votes
            from    @votes
            group by
                    stateid
            ,       voteeid
            ) cnt
    join    (
            select  stateid
            ,       max(votes) as maxvotes
            from    (
                    select  stateid
                    ,       voteeid
                    ,       count(*) as votes
                    from    @votes
                    group by
                            stateid
                    ,       voteeid
                    ) cnt2
            group by
                    stateid
            ) filter_max
    on      filter_max.stateid = cnt.stateid
            and filter_max.maxvotes = cnt.votes
    

    Example on SO Data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called sellerparams with a number of columns, one of them
So I have a table that links IP's to votes/flags of messages called tblIPMessageLink
i have table called type which contains data family,individual i want in some pages
In my oracle database I have table called PERSON with columns code, surname, forename.
I have table called view results in my db.it has 3 columns.. I want
Have a table called person. which has personid and contactid. if a person is
i have table called users , if i want to delete some user (User
I have a polymorphic table called Votes, where has votes from Answers and Questions.
I have table called users and I want to make an exact copy as
I have a mysql table called books that has a votes column, and another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.