最小和最大游戏数之间的差异

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 的游戏进行比较还是我应该使用而不是 countminmax 函数。如果有人能给我解释一下这个逻辑,将不胜感激。

表格:

player       competition
-------      --------
playerID     playerID
name         games
birthday     date
address      
telefon

应该是一个简单的查询:

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 个人来说可能是相似的。

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