Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Struggling with a query. Please help!
Old 09-23-2010, 12:08 PM Struggling with a query. Please help!
Junior Talker

Posts: 1
Name: Luis
Trades: 0
I have 2 different tables whose purpose is to store the points gotten by a group of users in a football game contest.

The 1st table stores the points gotten by the user for a prediction of the team players who will eventually play a game, and the 2nd one stores the points for a prediction of the final score of the game.

So both tables will store as many rows as users who predict, times as many games that have been played or to be played (one row of each football game for each user).

An example of how indistinctly one of these tables might look supposing that there are only three users and only three games:

Code:
userid   gameid      status      points
-----------------------------------------------
1          1             1         20
1          2             1         24
1          3             1         15
2          1             1         18
2          2             1         22
2          3             1         16
3          1             1         12
3          2             1         8
3          3             1         17
The other table would look similarly but will store different points.

What I want to get is a ranking with the total score of every user by adding up their stored points. I've been trying with several queries but I don't seem to get the expected result. For instance, this is the query I considered more accurate to what I wanted, but the totals are much bigger than expected:

SELECT ROUND(SUM(p.points) + SUM(r.points)) AS total, p.userid
FROM prediction_players p
INNER JOIN prediction_results r
ON p.userid = r.userid AND status=1
GROUP BY a.userid
ORDER BY total DESC

Note: the status field stored whether the corresponding game has already been played (1) or not (0). It must therefore be = 1 because otherwise points=NULL
luismartin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-25-2010, 12:56 AM Re: Struggling with a query. Please help!
rahulraj's Avatar
Ultra Talker

Posts: 456
Name: RAHUL RAJ
Location: Cochin, Kerala, India
Trades: 0
This query finds the sum in such a way that it just checks only for userid and status code. You don't want Cartesian product, right?
I guess you want GAME1_TOTAL + GAME2_TOTAL + GAME3_TOTAL per user, right?
You just add p.gameid = r.gameid

If you are not adding this condition, the addition will be done like,

For each row it will add values from multiple records in the second table. Because you are just checking the userid and status code. Thus game 1,2,3 points will be added with every p.userid (In the above table, userid 1 allotted in 3 rows; so for each row, sum of points will be calculated and added. Thus, it will be done three times! for calculating sum for user 1).
That's what I think..
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
rahulraj is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Struggling with a query. Please help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34720 seconds with 12 queries