1

所以我有一个排行榜,我每天使用以下查询有效地获取每个用户的分数:

SELECT DATE(a.time) as time, a.userid, SUM(activity_weight) as weight

FROM activity_entries a INNER JOIN users1 u ON u.id = a.userid 

WHERE competitionId = '$competitionId' GROUP BY a.userid, DATE(time) 

ORDER BY time ASC

我想知道,在整个比赛期间,找到“最稳定”表演者(即每天平均最高的用户)的有效方法是什么。

非常感谢!

编辑:对此进行测试,但遇到问题:

SELECT a.userid,
DATE(a.time) as time,
AVG(activity_weight) AS daily_average,

(SELECT a.userid, DATE(a.time) as time,
AVG(AVG(daily_average)) as topAverage
FROM activity_entries a INNER JOIN users1 u ON u.id = a.userid
WHERE competitionId = '$competitionId'
)

FROM activity_entries a INNER JOIN users1 u ON u.id = a.userid
WHERE competitionId = '$competitionId'
GROUP BY userid, time
4

1 回答 1

2

仅基于您提供的内容,并假设 activity_weight 是特定用户在单个事件中的得分,然后从以下内容开始:

SELECT userid,
       time,
       AVG(activity_weight) AS daily_average
FROM activity_entries
GROUP BY userid, time

此查询返回每个用户的平均每日得分。现在,我们需要定义“最一致”。如果可接受的定义是“每日平均值的最高平均值”,则将上述查询的内部联接作为子查询,选择AVG(daily_average).

如果“最一致”需要检查标准偏差,或者任何比平均值更复杂的数学运算,那么您应该在前端程序中进行,而不是在数据库中进行。

编辑:试试这个查询(或在这里玩弄它:SQLFiddle link):

SELECT davg.userid,
   SUM(davg.daily_average*davg.num_of_activities)/SUM(davg.num_of_activities) AS weighted_total_average
FROM (
  SELECT userid,
         time,
         AVG(activity_weight) AS daily_average,
     COUNT(*) AS num_of_activities
  FROM activity_entries
  GROUP BY userid, time
) AS davg
GROUP BY davg.userid
ORDER BY AVG(davg.daily_average) DESC

数学的实际细节由您决定,但这里有一个加权平均的例子,其中竞争对手参加大量比赛的日子比懒惰的日子更有价值。

于 2013-10-16T15:23:55.007 回答