[thelist] SQL Query to count votes and order by highest

Tab Alleman talleman at Lumpsum.com
Mon Dec 4 09:01:57 CST 2006


And if you want All Awards and the number of votes for each student all in one query:

SELECT
	vote_award_id
,	vote_to_student_id
,	COUNT(*) AS vote_count
FROM vote
GROUP BY 
	vote_award_id
,	vote_to_student_id
ORDER BY 
	vote_award_id
,	vote_to_student_id


> -----Original Message-----
> From: thelist-bounces at lists.evolt.org
> [mailto:thelist-bounces at lists.evolt.org]On Behalf Of Phil Turmel
> Sent: Sunday, December 03, 2006 4:45 PM
> To: thelist at lists.evolt.org
> Subject: Re: [thelist] SQL Query to count votes and order by highest
> 
> 
> Ken Snyder wrote:
> > Chris Byrd wrote:
> > 
> >>I'm part way through creating an online voting system for 
> various school
> >>'awards' and I need a query to count the number of votes 
> for a specific
> >>'award' and list the results in descending order 
> >> 
> >>Each time there's a vote, a row is created in a 'vote' table
> >> 
> >>Amongst other fields in the 'vote' table are 
> 'vote_to_student_id' and
> >>'vote_award_id'
> >> 
> >>Basically, I need to be able to run a query for each 'award_id' that
> >>will return (in descending order) the number of votes and each
> >>corresponding vote_to_student_id
> >>
> >>  
> > 
> > This worked for me on MS SQL 2005.
> > 
> > SELECT vote_award_id, COUNT(vote_to_student_id) as VoteCount
> > FROM vote
> > GROUP BY vote_award_id
> > ORDER BY COUNT(vote_to_student_id) DESC
> > 
> > -- Ken
> > 
> Ken,
> 
> While that would run, I don't think it gives the answer Chris 
> is looking 
> for.  It returns the list of elections, in descending order of voter 
> turnout.
> 
> Chris,
> 
> Given @ID for a single award:
> 
> SELECT vote_to_student_id, COUNT(*) as VoteCount
> FROM vote
> WHERE vote_award_id = @ID
> GROUP BY vote_to_student_id
> ORDER BY VoteCount DESC
> 
> will likely be what you are after.
> 
> HTH,
> 
> Phil
> -- 
> 
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
> 
> For unsubscribe and other options, including the Tip Harvester 
> and archives of thelist go to: http://lists.evolt.org 
> Workers of the Web, evolt ! 
> 



More information about the thelist mailing list