Reputation:
I currently have query like this:
SELECT 'id', 'clanid', 'name',
'level', 'exp', 'warwinpercent',
'warswon', 'warslost', 'warstied',
'playercount', 'score'
FROM clans
WHERE warswon >= 100
ORDER BY warwinpercent DESC, warswon DESC;
Now that works but in the end it isn't as logical as I would hope it could be...
For example.
Let's say there is a row which has 99.5738% war win percent and 208 wars won. And another which has 100% war win percent and 103 wars won.
I'd want the 99% row to be above the 100% row. Is there any way for me to get this working?
An equation I'd have is:
warinpercent = warswon/(warswon+warstied+warlost)*100
order by warwinpercent
if warwinpercent are in a range of 3% then order by warswon between them.
Upvotes: 4
Views: 161
Reputation: 175924
I suggest using ordering like in How Not To Sort By Average Rating
PROBLEM:
You need some sort of "score" to sort by.
WRONG SOLUTION #1: Score = (Positive ratings) - (Negative ratings)
WRONG SOLUTION #2: Score = Average rating = (Positive ratings) / (Total ratings)
CORRECT SOLUTION: Score = Lower bound of Wilson score confidence interval for a Bernoulli parameter
Demo:
CREATE TABLE clans(id INT, name VARCHAR(100), warswon INT, warslost INT);
INSERT INTO clans VALUES (1, 'aaa', 208, 6), (2, 'bbb', 103, 0);
SELECT id, name,warswon, warslost,((warswon + 1.9208) / (warswon + warslost) -
1.96 * SQRT((warswon * warslost) / (warswon + warslost) + 0.9604) /
(warswon + warslost)) / (1 + 3.8416 / (warswon + warslost))
AS ci_lower_bound
FROM clans
ORDER BY ci_lower_bound DESC;
Output:
╔═════╦═══════╦══════════╦═══════════╦════════════════════╗
║ id ║ name ║ warswon ║ warslost ║ ci_lower_bound ║
╠═════╬═══════╬══════════╬═══════════╬════════════════════╣
║ 2 ║ bbb ║ 103 ║ 0 ║ 0.9640439675800224 ║
║ 1 ║ aaa ║ 208 ║ 6 ║ 0.9401908847803808 ║
╚═════╩═══════╩══════════╩═══════════╩════════════════════╝
Upvotes: 7