获取每位玩家根据日期获得最高分数的次数

时间:2018-01-08 06:08:14

标签: group-by max

我想根据created_at找出每个玩家有多少次最高分数。 在下表中。

player_id 1在日期12和13上有两次最高分。 player_id 2在14日有一次最高分。

所以我想检查一下player_id = 1.这个玩家获得最高分数的时间。

+-----------+-------+-------------+------------+
| player_id | score |   sector    | created_at |
+-----------+-------+-------------+------------+
|         1 |    25 | Midfielder  | 2017-12-12 |
|         2 |    23 | Defender    | 2017-12-12 |
|         3 |    24 | Goalkeeper  | 2017-12-12 |
|         4 |    20 | Midfielder  | 2017-12-12 |
|         1 |    26 | Midfielder  | 2017-12-13 |
|         2 |    20 | Defender    | 2017-12-13 |
|         3 |    19 | Goalkeeper  | 2017-12-13 |
|         4 |    16 | Midfielder  | 2017-12-13 |
|         1 |    18 | Midfielder  | 2017-12-14 |
|         2 |    20 | Defender    | 2017-12-14 |
|         3 |    12 | Goalkeeper  | 2017-12-14 |
+-----------+-------+-------------+------------+

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

SELECT PLAYER_ID, COUNT(*) MAX_SCORE_COUNT
FROM
(
  SELECT player_id PLAYER_ID FROM YOUR_TABLE_NAME a
  WHERE a.score = ( SELECT MAX(b.score) FROM YOUR_TABLE_NAME b WHERE b.created_at = a.created_at)
) s
GROUP BY PLAYER_ID

此查询返回每位玩家根据created_at获得最高分数的次数。

如果您想查看player_id = 1,那么您可以使用,

SELECT PLAYER_ID, COUNT(*) MAX_SCORE_COUNT
FROM
(
  SELECT player_id PLAYER_ID FROM YOUR_TABLE_NAME a
  WHERE a.score = ( SELECT MAX(b.score) FROM YOUR_TABLE_NAME b WHERE b.created_at = a.created_at)
) s
WHERE PLAYER_ID = 1
GROUP BY PLAYER_ID