根据包括空值在内的最大条件获取唯一记录
Getting unique record based on max conditions including null values
我需要取回一位玩家(仅限 A 级玩家)最近获胜日期的记录(一些获胜日期为空,但我们需要包括它们)但只选择他们最近游戏会话的最后一个位置.所以基本上按照这个顺序:得到他们的最大值 win_date (如果为空,仍然包括它们)> 从那里抓住他们的最大位置 > 从那里,只选择他们的最大值 game_session_id.
Table 玩家:
Badge_No Name Game_Session_ID Place Win_Date Rank
565 Barry 012550 4 6/17/2021 A
565 Barry 003521 2 3/04/2021 A
565 Barry 003521 3 3/04/2021 A
565 Barry 003521 4 3/04/2021 A
565 Barry 003521 5 3/04/2021 A
565 Barry 095945 1 6/17/2021 A
101 Lee 065411 1 A
018 Jess 001561 1 5/23/2020 A
018 Jess 002075 1 5/23/2020 A
209 Linda 026541 2 5/06/2021 A
728 Perry 000940 1 1/23/2021 B
预期输出:
Badge_No Name Game_Session_ID Place Win_Date Rank
565 Barry 012550 4 6/17/2021 A
101 Lee 065411 1 A
018 Jess 002075 1 5/23/2020 A
209 Linda 026541 2 5/06/2021 A
我的(错误)代码:
select distinct badge_no,
name, max(game_session_id) game_session_id,
max(place) place, max(win_date) win_date, rank
from players p
where not exists
(select 'x' from players p2
where p2.badge_no = p.badge_no and p.rank = 'B')
group by badge_no, name, rank
将 ROW_NUMBER
与适当的分区一起使用:
WITH cte AS (
SELECT p.*, ROW_NUMBER() OVER (PARTITION BY Badge_No
ORDER BY Win_Date DESC, Place DESC, Game_Session_ID DESC) rn
FROM players p
WHERE "Rank" = 'A'
)
SELECT Badge_No, Name, Game_Session_ID, Place, Win_Date, "Rank"
FROM cte
WHERE rn = 1;
select badge_no, name,
max(game_session_id) keep (dense_rank last
order by win_date nulls first, place) as game_session_id,
max(place) keep (dense_rank last order by win_date nulls first) as place,
max(win_date) as win_date, rank
from players
where rank = 'A'
group by badge_no, name, rank
;
如果您不熟悉 first / last
聚合函数(不用担心,您不会孤单!),您可能需要快速浏览一下文档以了解它的作用。
我需要取回一位玩家(仅限 A 级玩家)最近获胜日期的记录(一些获胜日期为空,但我们需要包括它们)但只选择他们最近游戏会话的最后一个位置.所以基本上按照这个顺序:得到他们的最大值 win_date (如果为空,仍然包括它们)> 从那里抓住他们的最大位置 > 从那里,只选择他们的最大值 game_session_id.
Table 玩家:
Badge_No Name Game_Session_ID Place Win_Date Rank
565 Barry 012550 4 6/17/2021 A
565 Barry 003521 2 3/04/2021 A
565 Barry 003521 3 3/04/2021 A
565 Barry 003521 4 3/04/2021 A
565 Barry 003521 5 3/04/2021 A
565 Barry 095945 1 6/17/2021 A
101 Lee 065411 1 A
018 Jess 001561 1 5/23/2020 A
018 Jess 002075 1 5/23/2020 A
209 Linda 026541 2 5/06/2021 A
728 Perry 000940 1 1/23/2021 B
预期输出:
Badge_No Name Game_Session_ID Place Win_Date Rank
565 Barry 012550 4 6/17/2021 A
101 Lee 065411 1 A
018 Jess 002075 1 5/23/2020 A
209 Linda 026541 2 5/06/2021 A
我的(错误)代码:
select distinct badge_no,
name, max(game_session_id) game_session_id,
max(place) place, max(win_date) win_date, rank
from players p
where not exists
(select 'x' from players p2
where p2.badge_no = p.badge_no and p.rank = 'B')
group by badge_no, name, rank
将 ROW_NUMBER
与适当的分区一起使用:
WITH cte AS (
SELECT p.*, ROW_NUMBER() OVER (PARTITION BY Badge_No
ORDER BY Win_Date DESC, Place DESC, Game_Session_ID DESC) rn
FROM players p
WHERE "Rank" = 'A'
)
SELECT Badge_No, Name, Game_Session_ID, Place, Win_Date, "Rank"
FROM cte
WHERE rn = 1;
select badge_no, name,
max(game_session_id) keep (dense_rank last
order by win_date nulls first, place) as game_session_id,
max(place) keep (dense_rank last order by win_date nulls first) as place,
max(win_date) as win_date, rank
from players
where rank = 'A'
group by badge_no, name, rank
;
如果您不熟悉 first / last
聚合函数(不用担心,您不会孤单!),您可能需要快速浏览一下文档以了解它的作用。