我只想要每个赛季获胜最多的顶级球队。
我想要我的结果集为赛季:1900队名称:“a”wins:4 and 就像1901年的球队:“b”wins:5
如果有两支球队赢得相同的胜利,那么我的查询应该按照字母顺序选择第一支,就像在我的例子中,它将选择1900赛季的球队:“a”
源查询
create table #teams( season varchar(100), team_name varchar(100),win int);
insert #teams (season , team_name ,win)
select '1900','a' , 4;
insert #teams (season , team_name ,win)
select '1900','b' , 4;
insert #teams (season , team_name ,win)
select '1900','c' , 2;
insert #teams (season , team_name ,win)
select '1901','a' , 3;
insert #teams (season , team_name ,win)
select '1901','b' , 5;
insert #teams (season , team_name ,win)
select '1901','c' , 5;
select * from #teams;
select season ,team_name , max(win) as wins
from #teams
group by season, team_name
order by season
2条答案
按热度按时间5vf7fwbs1#
以下操作应该有效。。
我是按win desc的编号排序的,也按球队名称asc排序的,这应该符合选择在最终名单中按字母顺序排列的优秀球队的条件。
全分贝小提琴链接
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=abd425477e893e3dd869467daab4f216
suzh9iv82#
有几种方法: