Count the "ratio?" for wins and losses
I've already got this to work but it's a really bad approach and i need some help with factorization of my query.
SELECT `GameDate`,
COUNT(CASE
WHEN `P1Outcome`= 'win' AND P2Param = 'a' THEN 1
END) AS a_win,
COUNT(CASE
WHEN `P1Outcome`= 'loss' AND P2Param = 'a' THEN 1
END) AS a_loss,
COUNT(CASE
WHEN `P1Outcome`= 'win' AND P2Param = 'b' THEN 1
END) AS b_win,
COUNT(CASE
WHEN `P1Outcome`= 'loss' AND P2Param = 'b' THEN 1
END) AS b_loss
FROM games
WHERE `P2Param` IN ( 'a', 'b', 'c' )
GROUP BY GameDate
This will get me an query that i can use in my php application but i would like to skip having to make the actual ratio calculation in php and fetch it directly with SQL.
So basically what i've been trying to do is something similar to this:
COUNT(CASE
WHEN `P1Outcome`= 'win' AND P2Param = 'a' THEN 1 Else -1
END) AS a_ratio,
But just as the beginer i'm, i can't figure it out how i can make this to work.
EDIT: Sorry for not explaining my regards in more details, here is the thing. I'm creating an statistics component and i need to fetch the ratio for a period of time in order to display it as an graph. So the following things are required:
GameDate (1,2,3,4... days ago) The ratio for the player based on to different params (maps in this case)
So in short this is what i got at the moment:
GameDate a_win a_loss b_win b_loss
2011/04/25 x x x x
2011/04/23 x x x x
....
开发者_Go百科So everything works out great, but i would like to have the actual ratio calculation made in SQL because at the moment i need to make it in php e.g $ratio = $q[a_win]-$q[a_loss]
and due to the fact that I've a lot of different param my query is like double the size because i need to fetch both the win and loss instead of just the ratio like i want in the first place.
SELECT `GameDate`,
SUM(CASE `P2Param`
WHEN 'a' THEN CASE P1Outcome WHEN 'win' THEN 1 ELSE -1 END
ELSE 0
END) AS a_ratio,
SUM(CASE `P2Param`
WHEN 'b' THEN CASE P1Outcome WHEN 'win' THEN 1 ELSE -1 END
ELSE 0
END) AS b_ratio
FROM games
WHERE `P2Param` IN ( 'a', 'b', 'c' )
GROUP BY GameDate
What is very important COUNT
just returns number of rows regardless of what you count unless you use COUNT(DISTINCT ...)
which in turn returns count of distinct values. To sum up some values which you produce for every row use SUM(...)
. Make sure what happens in your DBMS when an expression in the SUM()
function evaluates to NULL
- some databases will just make the whole SUM return NULL
.
To get ratio of any kind of data you can do:
select _date_, (SUM(case when _your_test_ then 1 else 0 end) / count(1)) as ratio
from _yout_table_
group by _date_
And if you just want to calculate a sum of some values:
select _date_, SUM(case when _your_test_ then 1 else 0 end) as number_of_something
from _yout_table_
group by _date_
What about:
SELECT COUNT(id), P1Outcome, P2Param
FROM games
GROUP BY P2Param, P1Outcome
It will returns you a resultset with 3 columns:
- counted values for param and outcome
- outcome
- param
With that you can do more in my opinion.
SELECT GameDate, P2Param, SUM(CASE WHEN P1Outcome = 'win' THEN 1 ELSE -1 END) AS ratio
FROM games
GROUP BY GameDate, P2Param
精彩评论