How do you group by one column and retrieve a row with the minimum value of another column in T/SQL?

前端 未结 6 2039
终归单人心
终归单人心 2021-01-02 00:55

So I know this is a pretty dumb question, however (as the rather lengthily title says) I would like to know how do the following:

I have a table like this:



        
6条回答
  •  梦毁少年i
    2021-01-02 01:13

    declare @Borg table (
        ID int,
        Foo int,
        Bar int,
        Blagh int
    )
    insert into @Borg values (1,10,20,30)
    insert into @Borg values (2,10,5,1)
    insert into @Borg values (3,20,50,70)
    insert into @Borg values (4,20,75,12)
    
    select * from @Borg
    
    select Foo,Bar,Blagh from @Borg b 
    where Bar = (select MIN(Bar) from @Borg c where c.Foo = b.Foo)
    

提交回复
热议问题