11

我当前的应用程序根据每个用户的所有记录计算点平均值:

SELECT `user_id`, AVG(`points`) AS pts 
FROM `players` 
WHERE `points` != 0 
GROUP BY `user_id`

业务需求发生了变化,我需要根据每个用户的最后 30 条记录计算平均值。

相关表的结构如下:

表:玩家;列:player_id、user_id、match_id、点

表:用户;列:user_id

以下查询不起作用,但它确实演示了我试图实现的逻辑。

SELECT @user_id := u.`id`, (
    -- Calculate the average for last 30 records
    SELECT AVG(plr.`points`) 
    FROM (
        -- Select the last 30 records for evaluation
        SELECT p.`points` 
        FROM `players` AS p 
        WHERE p.`user_id`=@user_id 
        ORDER BY `match_id` DESC 
        LIMIT 30
    ) AS plr
) AS avg_points 
FROM `users` AS u

是否有一种相当有效的方法可以根据每个用户的最新 30 条记录计算平均值?

4

5 回答 5

11

没有理由重新发明轮子并冒着遇到错误、次优代码的风险。您的问题是常见的每组限制问题的微不足道的扩展。已经有经过测试和优化的解决方案来解决这个问题,我建议从这个资源中选择以下两种解决方案。这些查询为每个玩家生成最新的 30 条记录(为您的表重写):

select user_id, points
from players
where (
   select count(*) from players as p
   where p.user_id = players.user_id and p.player_id >= players.player_id
) <= 30;

(只是为了确保我理解您的结构:我想player_id是玩家表中的唯一键,并且一个用户可以作为多个玩家出现在此表中。)

第二个经过测试和优化的解决方案是使用 MySQL 变量:

set @num := 0, @user_id := -1;

select user_id, points,
      @num := if(@user_id = user_id, @num + 1, 1) as row_number,
      @user_id := user_id as dummy
from players force index(user_id) /* optimization */
group by user_id, points, player_id /* player_id should be necessary here */
having row_number <= 30;

第一个查询不会是最优的(二次的),而第二个查询是最优的(一次通过),但只能在 MySQL 中工作。这个选择由你。如果您选择第二种技术,请注意并使用您的密钥和数据库设置正确测试它;他们建议在某些情况下它可能会停止工作

您的最终查询很简单:

select user_id, avg(points)
from ( /* here goes one of the above solutions; 
          the "set" commands should go before this big query */ ) as t
group by user_id

请注意,我没有在您的第一个查询中包含您的条件,(points != 0)因为我不太了解您的要求(您没有描述它),而且我还认为这个答案应该足够笼统,以帮助其他有类似问题的人。

于 2013-06-08T17:12:14.823 回答
8

尝试这个:

SELECT user_id, AVG(points) AS pts 
FROM (SELECT user_id, IF(@uid = (@uid := user_id), @auto:=@auto + 1, @auto := 1) autoNo, points
      FROM players, (SELECT @uid := 0, @auto:= 1) A 
      WHERE points != 0 
      ORDER BY user_id, match_id DESC
     ) AS A 
WHERE autoNo <= 30
GROUP BY user_id;
于 2013-06-08T13:50:15.003 回答
0

这应该有效:

SELECT p1.user_id, avg(points) as pts
  FROM players p1, (
    SELECT u.user_id, (
         SELECT match_id
           FROM players p2
          WHERE p2.user_id = u.user_id
          ORDER BY match_id DESC
          LIMIT 29, 1 ) mid
      FROM users u
    HAVING mid IS NOT NULL) m
 WHERE p1.user_id = m.user_id
   AND p1.match_id >= m.mid
 GROUP BY p1.user_id

 UNION ALL

SELECT user_id, avg(points) AS pts 
  FROM players
 GROUP BY user_id
HAVING count(*) < 30

UNION ALL只有当您需要包含少于 30 条记录的用户时,才需要后面的部分。

于 2013-06-07T23:06:05.030 回答
0
SELECT 
u.`id`, 
(SELECT AVG(p.`points`) FROM FROM `players` AS p WHERE p.`user_id`=u.`id` 
ORDER BY p.`user_id` DESC LIMIT 30) AS AVG
FROM `users` AS u Group by u.`id`

也试试这个...

于 2013-06-08T06:45:02.993 回答
0

如果我正确理解您的逻辑,您需要根据最后 30 条非零分记录(按 match_id 排序)计算每个用户的平均得分。

首先,您需要返回每个用户的最后 30 条记录,您可以使用如下查询:

SELECT p.user_id, p.match_id, p.points
FROM
  players p INNER JOIN players c
  ON p.user_id=c.user_id AND p.match_id<=c.match_id
     AND p.points!=0 and c.points!=0
GROUP BY
  p.user_id, match_id, points
HAVING
  COUNT(c.user_id)<=30

然后你需要计算上一个查询的平均值:

SELECT user_id, AVG(points)
FROM (
  SELECT p.user_id, p.match_id, p.points
  FROM
    players p INNER JOIN players c
    ON p.user_id=c.user_id AND p.match_id<=c.match_id
       AND p.points!=0 and c.points!=0
  GROUP BY
    p.user_id, match_id, points
  HAVING
    COUNT(c.user_id)<=30
  ) l
GROUP BY user_id
于 2013-06-08T08:17:15.553 回答