tags:

views:

205

answers:

7

Hi :)

I have a query:

SELECT COUNT(*) as votes, a.member_id 
FROM ballots a
WHERE ballot_id = 1
GROUP BY a.member_id

which yields something like:

votes  member_id
1      paul
5      mike
3      noynoy
10     andy
2      noel

I'd like to be able to get the row "andy" because he got the highest "votes".

How do I change my query to do this?

Thanks in advance for your help :)

+2  A: 
SELECT COUNT(*) as votes, a.member_id 
FROM ballots a
WHERE ballot_id = 1
GROUP BY a.member_id
ORDER BY COUNT(*) DESC 
LIMIT 1
Scott Anderson
This won't count up all the votes for a user, there is no group by
Hogan
yeah I fixed it to match his query more exactly.
Scott Anderson
+2  A: 
SELECT COUNT(*) as votes, a.member_id 
FROM ballots a 
WHERE ballot_id = 1 
GROUP BY a.member_id
ORDER BY votes DESC
LIMIT 1
Sander Rijken
TOP is MS SQL, use LIMIT 1 at the end.
Jimmy Shelter
Woops, you're right!
Sander Rijken
Now it's correct you get my last upvote for the day ;)
Jimmy Shelter
+1  A: 
SELECT COUNT(*) as votes, a.member_id 
FROM ballots a 
WHERE ballot_id = 1 
GROUP BY a.member_id
ORDER BY COUNT(*) DESC
LIMIT 1
Hogan
+5  A: 

You can order it from highest to lowest votes using ORDER BY and then DESC for descending order. Then LIMIT the results to only one row (i.e. the highest).

SELECT COUNT(*) as votes, a.member_id 
FROM ballots a
WHERE ballot_id = 1
GROUP BY a.member_id
ORDER BY votes DESC
LIMIT 1
Rich Adams
+1  A: 

Using COUNT() will not select all the rows if the maximum value repeats. My suggestion is to use MAX()

SELECT *
FROM ballots a
WHERE votes = (SELECT max(votes) FROM ballots)
Clash
This is wrong -- each user can have more than one row with votes.
Hogan
A: 

If you use Limit 1 how will you know if there was a Tie for first?

SELECT      
     Count_Result.votes,
     Count_Result.member_id   
FROM
     (Select        
          Count(*)  as votes, 
          a.member_id   
     FROM       
          ballots a
     WHERE 
          ballot_id = 1     
     GROUP BY 
          a.member_id) Count_Result     
     INNER JOIN 
     (SELECT            
          MAX(votes)votes
     FROM       
          (Select 
               Count(*)  as votes, 
               a.member_id          
          FROM
               ballots a
          WHERE 
               ballot_id = 1            
          GROUP BY 
               a.member_id ) tr     
          )     max_votes 
     ON Count_Result.votes = max_votes.votes

Alternative using the having clause

Select 
    Count(*)  as votes, 
    a.member_id   
FROM
    ballots  a
WHERE
    ballot_id = 1 
GROUP BY
    a.report_date
HAVING 
    Count(*) = 
    (SELECT            
        MAX(votes)votes
    FROM       
        (Select 
                Count(*)  as votes, 
                a.member_id   
        FROM
                ballots  a
        WHERE 
            ballot_id = 1 
        GROUP BY 
            a.member_id   e ) tr     
          )    
Conrad Frix
That's a lot of subqueries that are not really necessary. What about zero subqueries and a HAVING clause?
recursive
I don't see how you can do it with ZERO subqueries since you're looking for the Max of the Count not just the max of a field. But if you like the having clause better I've added an alternative
Conrad Frix
A: 

might be easier to get with a dedicated view for counting the votes

CREATE VIEW v_votes AS 
  SELECT member_id, ballot_id,  COUNT(*) AS votes FROM ballots GROUP BY member_id

and then use a HAVING clause

SELECT * FROM v_votes HAVING MAX(votes)