我有一个看起来像这样的表:
Player | Sport | Rank
A Tennis 1
B Tennis 2
C Tennis 2
D Tennis 2
E Tennis 3
F Soccer 1
G Soccer 2
以此类推。
对于Sport相同的每个分区,我希望将结果过滤掉,以使每个Sport的每个排名都是唯一的。在上面的示例中,结果中选择了B / C / D之一,而我不在乎选择了哪个。
我希望结果是(其中D也可以是B或C):
Player | Sport | Rank
A Tennis 1
D Tennis 2
E Tennis 3
F Soccer 1
G Soccer 2
如何在SQL中实现此逻辑?
答案 0 :(得分:3)
一个好的老式GROUP BY应该这样做:
SELECT MIN(player) AS any_player, sport, rank
FROM t
GROUP BY sport, rank
答案 1 :(得分:1)
您可以使用ROW_NUMBER:
Select player, sport,rank
FROM
(select player, sport,rank,
ROW_NUMBER() OVER(partition by sport,rank order by player) as rown
FROM table
)a
WHERE a.rown=1;