对于名为score
的虚表,请考虑以下表结构:
player_name |player_lastname |try |score
primary key: (player_name,player_lastname,try)
(不要讨论表模式,这只是一个例子)
这个表包含所有玩家的分数-每个玩家都应该可以玩一次或两次。现在,我怎样才能只获取每个玩家最后一次尝试的数据(即,对于那些玩了不止一次的玩家,应该忽略第一次尝试)?
我正在努力实现的一个例子:
player_name,player_lastname,try,score
=====================================
bart, simpson,1,250
lisa,simpson,1,150
lisa,simpson,2,250
homer,simpson,1,300
homer,simpson,2,350
maggi,simpson,1,50
结果应该是:
player_name,player_lastname,try,score
=====================================
bart, simpson,1,250
lisa,simpson,2,250
homer,simpson,2,350
maggi,simpson,1,50
最佳答案
秩函数可以解决这个问题:
SELECT player_name,player_lastname,TRY,score
FROM (SELECT player_name,player_lastname,TRY,score,RANK() OVER (PARTITION BY player_name, Player_Lastname ORDER BY TRY DESC)AS try_rank
FROM score
)sub
WHERE try_rank = 1
我假设“try”是1/2的数字。
编辑,忘记分区依据