【问题标题】:How can I count the average number closest to 100?如何计算最接近 100 的平均数?
【发布时间】:2012-12-19 23:17:10
【问题描述】:

我有一个 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

【问题讨论】:

  • 我认为您应该有一个子查询来查找差异,然后对其进行平均。
  • 你可能想要SUM(ABS(100-score)) AS TotalDifference
  • 感谢 Shmiddty,成功了!

标签: mysql


【解决方案1】:

应该这样做

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 将包含累积的差值

【讨论】:

    【解决方案2】:

    您可以修复查询以返回绝对值,以查看分数与给定值的距离(在您的情况下为 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
    

    【讨论】:

    • 我想说你的意思是AVG(ABS(100-score))
    • 根据示例,我认为操作想要对差异求和而不是平均
    • 是的,我认为@Damp 是正确的。我修改了我的答案,但他打败了我,得到了正确的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    相关资源
    最近更新 更多