如何选择具有最小值的不同行

时间:2017-10-15 00:28:03

标签: sql sql-server database

我想要一个SQL语句来获取具有最小值的行。

数据库

ig_idx    ig_team1  ig_team2  rl_league  rl_image          rl_sports   ig_status   cnt
410296   aaaaa      xxxxx     BRA D2     icon_BRA D2.png   7           S           3
410298   aaaaa      xxxxx     BRA D2     icon_BRA D2.png   7           S           3
410299   aaaaa      yyyyy     BRA D2     icon_BRA D2.png   7           S           3
410301   bbbbb      yyyyy     BRA D2     icon_BRA D2.png   7           S           2
410304   bbbbb      yyyyy     BRA D2     icon_BRA D2.png   7           S           2
410306   ccccc      zzzzz     BRA D2     icon_BRA D2.png   7           S           2

如何选择点列中具有最小值的ID?像这样...

ig_idx    ig_team1  ig_team2  rl_league  rl_image          rl_sports    ig_status   cnt
410296    aaaaa     xxxxx     BRA D2     icon_BRA D2.png   7            S            3
410301    bbbbb     yyyyy     BRA D2     icon_BRA D2.png   7            S            2
410306    ccccc     zzzzz     BRA D2     icon_BRA D2.png   7            S            2

2 个答案:

答案 0 :(得分:3)

使用row_number()

select t.*
from (select t.*,
             row_number() over (partition by ig_team order by ig_idx asc) as seqnum
      from t
     ) t
where seqnum = 1;

答案 1 :(得分:0)

看起来很简单:

SELECT MIN(ig_idx), ig_team1, ig_team2, r1_league, r1_image, r1_sports, ig_status, cnt
FROM t
GROUP BY ig_team1, ig_team2, r1_league, r1_image, r1_sports, ig_status, cnt