根据表Scores
,我想为给定的ID
选择max(score)
GID
,但如果得分重复,我想要它返回min(lastplayed)
日期的ID。
ID GID SCORE LASTPLAYED
== === ===== ==========
1 ABC 100 2017-12-13
2 ABC 95 2017-12-15
3 ABC 100 2017-12-22
我想要ID = 1
下面是我想要的东西,除了ID是唯一的,每行都返回。从选择中删除ID会在最早的日期给出最高分,但不幸的是,这不是我想要的。
最终目标是仅返回第1行的ID列。
select ID, max(Score), min(LastPlayed)
from Scores
where GId = 'ABC'
group by ID
答案 0 :(得分:1)
在row_number
中使用order by
等排名功能。
select top 1 with ties *
from tbl
order by row_number() over(partition by gid order by score desc,last_played)