简体   繁体   中英

how to select distinct row with minimum value

I want an SQL statement to get the row with a minimum value.

database

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

How do I select the ids that have the minimum value in the point column? like this...

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

Use 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;

Looks like it's as simple as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM