sql server select first row from a group

前端 未结 2 1241
遥遥无期
遥遥无期 2020-11-30 05:33

I have table like this:

a          b
1          23
1          2
1          7
2          9
2          11

I want to select the first row(orde

相关标签:
2条回答
  • 2020-11-30 06:00
    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
    

    see http://msdn.microsoft.com/en-us/library/ms186734.aspx

    0 讨论(0)
  • 2020-11-30 06:02

    If as you indicated, order doesn't matter, any aggregate function on b would be sufficient.

    Example Using MIN

    SELECT a, b = MIN(b)
    FROM   YourTable
    GROUP BY
           a
    
    0 讨论(0)
提交回复
热议问题