如何在 MySQL 中选择 TOP n 分组值的平均值?

发布于 2024-10-14 06:36:44 字数 1036 浏览 4 评论 0原文

数据库:

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 名球员仅平均球队得分,这就是我迷失的地方。

The Database:

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

I am trying to get the top 2 players from each team's points and average them towards the teams ranking, while grouping the teams in a query:

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");`

Is not working as expected.

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

Is listing the top teams, whom have at least 10 people in the db. Now adding in a way to get the top 15 players ONLY to average out the teams scores is where I am lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没结果 2024-10-21 06:36:44
SELECT charGuild, AVG(charRating) as gr FROM ins_rated GROUP BY charGuild order by gr desc limit 2 
SELECT charGuild, AVG(charRating) as gr FROM ins_rated GROUP BY charGuild order by gr desc limit 2 
面如桃花 2024-10-21 06:36:44

错误代码: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 个最高的独特分数。如果队伍中有 2 名玩家获得 100 分,则不会选择(100,100)作为最高分,而是选择(100,95)。

Error Code : 1235 This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

I think you might need to use some coding.

Best I could do is:

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));

outputs:

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

EDIT: Scratch that. It's getting the 2 highest unique scores. If 2 players on the team got 100, it would not choose (100,100) as the highest scores, but (100,95).

坐在坟头思考人生 2024-10-21 06:36:44

呜呼!

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 人。

Woohoo!

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

Outputs:

charGuild           average  
team 1              95.0000  
team 2              97.5000  

EDIT: Scratch that. This one assumes the number of teams is known, and that each team has at least 2 people.

寄意 2024-10-21 06:36:44

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

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      

I like this one:
It doesn't have any of the flaws of my other 2 answers...

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;

Output:

charGuild     average
team 1        95.0000         
team 2        97.5000      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文