
[英]How to find the difference of sum between maximum cumulative sum and minimum cumulative sum?
[英]The difference between the minimum and maximum number of games
问题:显示所有具有以下条件的玩家的姓名:该玩家的最小和最大游戏数之差大于 5。
select p.name
from player p
join competition c
on c.playerID = p.playerID
where (
(select count(*) from competition
where count(games) > 1
group by playerID
) - (
select count(*) from competition
where count(games) <= 1
group by playerID
))> 5;
我有点迷路了。 我不太确定这是正确的方法,我应该如何进行:我应该使用count并找到最小和最大游戏数并与大于 5 进行比较,还是应该使用count 、 min和max函数。 非常感谢,如果有人能解释一下这个逻辑。
表:
player competition
------- --------
playerID playerID
name games
birthday date
address
telefon
SELECT
P.Name,
MIN(C.Games) MinGame,
MAX(C.Games) MaxGame,
FROM Player P
INNER JOIN Competition C
ON C.PlayerId = P.PlayerId
GROUP BY P.Id, P.Name
HAVING MAX(C.Games) - MIN(C.Games) > 5
它应该是一个简单的查询:
With tab1 AS (Select player.name, min(games) mx_game, max(games) min_game,
max(games) - min(games) diff
from player JOIN competition ON player.player_id = competition.id
group by player.player_id, player.name)
Select tab1.name from tab1
WHERE diff >5;
我在组中添加 player_id,因为 player_name 可能与 2 人相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.