我有这样的表:
a b
1 23
1 2
1 7
2 9
2 11
我想从“GROUP BY a”查询中选择第一行(顺序无关紧要),结果应为
a b
1 23
2 9
我正在使用SQL SERVER 2008如何为此编写查询?
答案 0 :(得分:88)
select a,b
from (
select a,b,row_number() over(partition by a order by b desc) as roworder
from myTable
) temp
where roworder = 1
答案 1 :(得分:55)