I have a table with some stats. Example:
date userId attempts good bad 2010-08-23 1 5 4 1 2010-08-23 2 10 6 4 2010-08-23 3 6 3 3 2010-08-23 4 8 2 6
Each user has to do something, and the outcome is either good or bad. I'd like to find out what the relative score is for each user, compared to the other users on that day. Example:
User 1 made 5 attempts, and 4 of those were good. So 4 / 5 = 80%
of his attempts were good. For the other users that day it was 60%, 50% and 25%. So the relative score of successful attempts for user 1 on that day is 80 / (80 + 60 + 50 + 25) ≈ 37%
.
But I'm stuck at this point:
SELECT
date,
userId,
( (good / attempts) / x ) * 100 AS score_good
( (bad / attempts) / y ) * 100 AS score_bad
FROM stats
GROUP BY date, userId -- ?
Where x is the sum of all the (good / attempts)
for that day, and y is the sum of all the (bad / attempts)
for the same day. Can this be done in the same query?
I'd like the result to be e.g.
date userId score_good 2010-08-23 1 37% 2010-08-23 2 28% (60 / (80 + 60 + 50 + 25)) etc
Or:
userId score_good_total 1 ...
Where score_good_total
would be the sum of all the score_good
scores, divided by the amount of days.
I can replace x and y with a subquery, but that doesn't seem they way to go, and will probably cause too much of a load when I want the data grouped by month or totals scores for all the available dates.