SQL Server:使用MAX删除重复项
给定一个表,SQL Server:使用MAX删除重复项,sql,sql-server,Sql,Sql Server,给定一个表,Scores,我想为给定的GID的max(Scores)选择ID,但如果是重复的分数,我希望它返回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 下面是我想
Scores
,我想为给定的GID
的max(Scores)
选择ID
,但如果是重复的分数,我希望它返回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是唯一的,每一行都返回。从select中删除ID将为我提供最早日期的最高分数,但不幸的是,这不是我想要的
最终目标是只返回第1行的ID列
select ID, max(Score), min(LastPlayed)
from Scores
where GId = 'ABC'
group by ID
在
排序依据
中使用排序函数,如行数
select top 1 with ties *
from tbl
order by row_number() over(partition by gid order by score desc,last_played)