如何在 MySQL 中选择 TOP n 分组值的平均值?
数据库:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您可能需要使用一些编码。
我能做的最好的事情是:
输出:
编辑:从头开始。它获得了 2 个最高的独特分数。如果队伍中有 2 名玩家获得 100 分,则不会选择(100,100)作为最高分,而是选择(100,95)。
I think you might need to use some coding.
Best I could do is:
outputs:
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).
呜呼!
输出:
编辑:从头开始。此假设假设团队数量已知,并且每个团队至少有 2 人。
Woohoo!
Outputs:
EDIT: Scratch that. This one assumes the number of teams is known, and that each team has at least 2 people.
我喜欢这个:
它没有我其他两个答案的任何缺陷......
输出:
I like this one:
It doesn't have any of the flaws of my other 2 answers...
Output: