Jump to content

Very Tough Query


Mateobus

Recommended Posts

Very Tough Query

 

Hey all I have a tough query here. Basically I am making a top 25 poll, and I am trying to use two tables. The first table is the votes table and is set up as follows:

 

voterid | place | teamid

 

The second table should contain the number of points for each team from the votes table as well as the number of first place votes. Points can be calculated by the equation 26-place. So a first place vote gets 25 points, 2nd 24...

 

The second table is set up as follows

 

teamid | points | first_place_votes

 

The query should incorporate all of the votes from the first table into the second one.  If anyone can help I would greatly appreciate it. I could use php to rig this up with arrays and such, but I would rather have it done more efficiently.

Link to comment
Share on other sites

Very Tough Query

 

Hey all I have a tough query here. Basically I am making a top 25 poll, and I am trying to use two tables. The first table is the votes table and is set up as follows:

 

voterid | place | teamid

 

The second table should contain the number of points for each team from the votes table as well as the number of first place votes. Points can be calculated by the equation 26-place. So a first place vote gets 25 points, 2nd 24...

 

The second table is set up as follows

 

teamid | points | first_place_votes

 

The query should incorporate all of the votes from the first table into the second one.  If anyone can help I would greatly appreciate it. I could use php to rig this up with arrays and such, but I would rather have it done more efficiently.

 

You can get this output dynamically. If there are performance issues because of how frequent the page is being accessed or because of how much data there is, then caching the page that shows the output should be a viable option.

 

To answer your question

INSERT INTO
table2
(teamid, points, first_place_votes)
SELECT
teamid, SUM(26 - place) AS points, SUM(place = 1) AS first_place_votes
FROM
table1
GROUP BY
teamid

Link to comment
Share on other sites

So lets see if I understand... a single voter is going to vote for (up to) 25 teams? It looks like shoz has a pretty good handle on it. But I'd recommend you fill table two with your teams, 0 points and 0 first place votes and then after every vote, run what shoz gave you as an update query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.