我有一个MySQL表格,我从一个游戏中收集了一个数字列表,用户必须点击一个按钮才能接近100。
我以为我会使用AVG()
,但这显然不会起作用,因为当你有0和200时,它看起来好像你得了一个完美的100。
我需要的是每一轮(有10个)计算与数字相差100的距离并计算它们。
Example:
User A: 98 + 102 + 102 = 6 (something) Loser!
User B: 95 + 100 + 100 = 5 (something) Winner!
我有什么,但显然有缺陷:
SELECT user_id, AVG(score) AS Average, (100-AVG(score)) AS Difference,
DATE(playtime) AS Playdate
FROM playtable
GROUP BY user_id, DATE(playtime)
ORDER BY Playdate DESC, user_id
答案 0 :(得分:7)
这应该这样做
SELECT user_id, SUM( ABS(100-score) ) as cumulative_error,
DATE(playtime) AS Playdate
FROM playtable
GROUP BY user_id, DATE(playtime)
ORDER BY Playdate DESC, user_id
cumulative_error将包含累积的差值
答案 1 :(得分:1)
您可以修复查询以返回绝对值,以查看分数与给定值的距离(在您的情况下为100)
SELECT user_id, AVG(score) AS Average, SUM(ABS(100 - score)) AS Difference,
DATE(playtime) AS Playdate
FROM playtable
GROUP BY user_id, DATE(playtime)
ORDER BY Playdate DESC, user_id