如何在SQL中找到具有最大列值的行?

时间:2014-04-11 07:45:41

标签: sql sql-server

我有一张类似

的表格
phoneNumber | City      | totalPoints
12345678    | Singapore | 2000
12345679    | Singapore | 3000
23456789    | New York  | 2100
12312312    | New York  | 2200
12312343    | Beijing   | 4000

我希望得到像

这样的结果
phoneNumber | City      | totalPoints
12345679    | Singapore | 3000
12312312    | New York  | 2200
12312343    | Beijing   | 4000

只需选择每个城市中totalPoints最大值的行。如何编写SQL代码? (我正在使用MS SQL Server)

1 个答案:

答案 0 :(得分:3)

在sql-server中:

select * from
(
select *,rn=row_number()over(partition by City order by totalpoints desc) from table
)x
where rn=1