问题:显示具有以下条件的所有玩家的姓名: 最小和最大游戏数之间的差异 这个玩家大于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进行比较,还是应该使用< strong> count ,最小和最大功能。如果有人可以向我解释这个逻辑,将不胜感激。
表格:
player competition
------- --------
playerID playerID
name games
birthday date
address
telefon
答案 0 :(得分:0)
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
答案 1 :(得分:-1)
这应该是一个简单的查询:
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个人相似。