【问题标题】:How to SELECT an AVG of the TOP n grouped values in MySQL?如何在 MySQL 中选择 TOP n 分组值的 AVG?
【发布时间】:2011-01-27 12:50:38
【问题描述】:

数据库:

player  |  team  |  points  
player1 | team1  |  100
player2 | team1  |  90
player3 | team2  |  100
player4 | team2  |  95
player5 | team2  |  90

我试图从每支球队的积分中获得前 2 名球员,并将他们平均到球队排名中,同时在查询中对球队进行分组:

team2 97.5 (not 95)
team1 95


       `$mysqli->query("SELECT charGuild, gr FROM (
        SELECT charGuild, AVG(charRating) as gr 
        FROM ins_rated 
        GROUP BY charGuild 
        HAVING COUNT(*) >= 10
        ORDER BY gr DESC
        LIMIT 15
        )
       ORDER BY gr DESC
       LIMIT 40");`

没有按预期工作。

$mysqli->query("SELECT charGuild, AVG(charRating) AS gr 
                                        FROM ins_rated 
                                        GROUP BY charGuild 
                                        HAVING COUNT(*) >= 10
                                        ORDER BY gr DESC
                                        LIMIT 40");

列出在数据库中至少有 10 人的顶级团队。现在添加一种仅让前 15 名球员平均球队得分的方法是我迷路的地方。

【问题讨论】:

标签: php mysql group-by


【解决方案1】:
SELECT charGuild, AVG(charRating) as gr FROM ins_rated GROUP BY charGuild order by gr desc limit 2 

【讨论】:

  • 这并不完全符合我的要求。 charGuild 组需要组中前 15 名的限制器,但我想要一个所有 charGuild 或“团队”的大列表。
【解决方案2】:

错误代码:1235 此版本的 MySQL 尚不支持“LIMIT & IN/ALL/ANY/SOME 子查询”

我认为您可能需要使用一些编码。

我能做的最好的就是:

SELECT charGuild, charRating
FROM ins_rated a
WHERE charRating = (SELECT MAX(charRating) FROM ins_rated b WHERE b.charGuild = a.charGuild)
OR charRating = (SELECT MAX(charRating) FROM ins_rated b WHERE b.charGuild = a.charGuild
    AND charRating < (SELECT MAX(charRating) FROM ins_rated c WHERE c.charGuild = a.charGuild));

输出:

charGuild     charRating   
team 1        100          
team 1        90           
team 2        100          
team 2        95           

编辑:从头开始。它获得了 2 个最高的 unique 分数。如果队中有 2 名玩家得了 100,则不会选择 (100,100) 作为最高分,而是选择 (100,95)。

【讨论】:

    【解决方案3】:

    哇哦!

    SELECT charGuild, AVG(charRating) average
    FROM (SELECT charRating, charGuild FROM ins_rated a ORDER BY charRating DESC LIMIT 4) top4
    GROUP BY charGuild
    

    输出:

    charGuild           average  
    team 1              95.0000  
    team 2              97.5000  
    

    编辑:从头开始。这个假设团队的数量是已知的,并且每个团队至少有 2 人。

    【讨论】:

      【解决方案4】:

      我喜欢这个: 它没有我其他2个答案的任何缺陷......

      SELECT charGuild, AVG(charRating) average
      FROM (
          SELECT
              charRating,
              charGuild,
              @num := IF(@guild = charGuild, @num + 1, 1) AS row_number,
              @guild := charGuild
          FROM ins_rated a
              ORDER BY charGuild DESC
      ) ordered
      WHERE row_number <= 2
      GROUP BY charGuild;
      

      输出:

      charGuild     average
      team 1        95.0000         
      team 2        97.5000      
      

      【讨论】:

      • 此解决方案似乎在纸上有效,但由于某种原因在我的服务器上无法正常工作。我需要打开某个标志吗? @num+1 根本不起作用。每个@guild 的值都是 1,就好像它们都是唯一的拼写一样(它们不是分组...
      • insomniaguild.org/paul/test.php这里可以看到,嵌套查询没有执行+1...
      • 我使用的是 MySQL 5.1.36 版。也许它是新的……你能升级吗?
      • 今晚我会问我的网络主机。不过它是在两台不同的服务器上执行此操作的,所以我不确定交易是什么。
      猜你喜欢
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2011-06-12
      • 2016-01-18
      相关资源
      最近更新 更多