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

8mmmxcuj  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(354)

问:显示所有玩家的名字谁有以下:之间的差异,最小和最大的游戏数,这名球员是大于5。

  1. select p.name
  2. from player p
  3. join competition c
  4. on c.playerID = p.playerID
  5. where (
  6. (select count(*) from competition
  7. where count(games) > 1
  8. group by playerID
  9. ) - (
  10. select count(*) from competition
  11. where count(games) <= 1
  12. group by playerID
  13. ))> 5;

我有点迷路了。我不太确定这是正确的方法,我应该如何继续:我应该使用计数,找到游戏的最小和最大数量,并与大于5比较,还是我应该使用计数,最小和最大的函数,而不是计数。如果有人能给我解释一下其中的逻辑,我将不胜感激。
table:

  1. player competition
  2. ------- --------
  3. playerID playerID
  4. name games
  5. birthday date
  6. address
  7. telefon
kognpnkq

kognpnkq1#

  1. SELECT
  2. P.Name,
  3. MIN(C.Games) MinGame,
  4. MAX(C.Games) MaxGame,
  5. FROM Player P
  6. INNER JOIN Competition C
  7. ON C.PlayerId = P.PlayerId
  8. GROUP BY P.Id, P.Name
  9. HAVING MAX(C.Games) - MIN(C.Games) > 5
yshpjwxd

yshpjwxd2#

它应该是一个简单的查询:

  1. With tab1 AS (Select player.name, min(games) mx_game, max(games) min_game,
  2. max(games) - min(games) diff
  3. from player JOIN competition ON player.player_id = competition.id
  4. group by player.player_id, player.name)
  5. Select tab1.name from tab1
  6. WHERE diff >5;

我正在添加组中的玩家\id,因为2个人的玩家\u名称可能类似。

相关问题