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 9179429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:47:38+00:00 2026-06-17T17:47:38+00:00

I have a database in which people submit votes for people in different places.

  • 0

I have a database in which people submit votes for people in different places. At a given time, I want to find out who has the most votes at each place. (A person can be voted at two different places)

This is the SQL I have so far:

SELECT placeId, userVotedId, cnt 
FROM 
    (SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
     FROM users as u, users_votes as uvo, places as p 
     WHERE u.userId = uvo.userVotedId 
       AND p.placeId = uvo.placeId 
     GROUP BY userVotedId, placeId) 
AS RESULT

which gives me this result:

enter image description here

Now, these are the rows I REALLY want:

enter image description here

What’s missing in my query so I can get this?

  • I want one result per place. So I should see only distinct placeIds, with the userVotedId who received the most votes.

  • In the event of a tie, a random winner will do!

  • 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-06-17T17:47:40+00:00Added an answer on June 17, 2026 at 5:47 pm

    Seems like you need one more aggregate. Use the MAX() aggregate on your cnt value and GROUP BY placeId, userVotedId:

    SELECT placeId, userVotedId, max(cnt)
    FROM 
    (
      SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
      FROM users as u
      INNER JOIN users_votes as uvo
        ON u.userId = uvo.userVotedId 
      INNER JOIN places as p 
        ON p.placeId = uvo.placeId 
      GROUP BY userVotedId, placeId
    ) AS RESULT
    GROUP BY placeId, userVotedId
    

    Note: I changed your query to use JOIN syntax instead of the commas between the tables.

    Edit, based on your comment the following should work:

    select total.uservotedid,
      total.placeid,
      total.cnt
    from
    (
      SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
      FROM users as u
      INNER JOIN users_votes as uvo
        ON u.userId = uvo.userVotedId 
      INNER JOIN places as p 
        ON p.placeId = uvo.placeId 
      GROUP BY userVotedId, placeId
    ) total
    inner join
    (
      select max(cnt) Mx, placeid
      from
      (
        SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
        FROM users as u
        INNER JOIN users_votes as uvo
          ON u.userId = uvo.userVotedId 
        INNER JOIN places as p 
          ON p.placeId = uvo.placeId 
        GROUP BY userVotedId, placeId
      ) mx
      group by placeid
    ) src
      on total.placeid = src.placeid
      and total.cnt = src.mx
    

    See SQL Fiddle with Demo

    The result is:

    | USERVOTEDID | PLACEID | CNT |
    -------------------------------
    |          65 |      11 |   1 |
    |          67 |      13 |   1 |
    |          67 |      25 |   1 |
    |          67 |      51 |   2 |
    

    Edit #2, if you want a random number returned if there is a tie, then you can use user variables:

    select uservotedid,
      placeid, 
      cnt
    from
    (
      select total.uservotedid,
        total.placeid,
        total.cnt,
        @rownum := case when @prev = total.placeid then @rownum+1 else 1 end rownum,
        @prev := total.placeid pplaceid
      from
      (
        SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
        FROM users as u
        INNER JOIN users_votes as uvo
          ON u.userId = uvo.userVotedId 
        INNER JOIN places as p 
          ON p.placeId = uvo.placeId 
        GROUP BY userVotedId, placeId
      ) total
      inner join
      (
        select max(cnt) Mx, placeid
        from
        (
          SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
          FROM users as u
          INNER JOIN users_votes as uvo
            ON u.userId = uvo.userVotedId 
          INNER JOIN places as p 
            ON p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId
        ) mx
        group by placeid
      ) src
        on total.placeid = src.placeid
        and total.cnt = src.mx
      order by total.placeid, total.uservotedid
    ) src
    where rownum = 1
    order by placeid, uservotedid
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a table in my database which has duplicate records that I want
I have a database which has a table recording when a vehicle was last
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have a database which people can log into to enter information. What I
we have a view in our database which has an ORDER BY in it.
I have a database which, simiplified, is as follows : ID | SecondID |
I have a database which is going to have UMT timestamps in standard sql
I have a database which contains the link to audio files. I am trying
i have a database which contains an arabic tables , i can read it
I have a database which I regularly need to import large amounts of data

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.