选择组后多个列的最大计数

时间:2018-05-21 20:38:09

标签: mysql sql database

我有一个包含架构和数据的表,如下所示

OverlayState

等等。

基本上,每个赛季的每场比赛都会有一个参赛作品。

我需要查询并确定每个赛季赢得最多比赛数的球队。

我直到检索每个赛季的最大比赛数,但我无法找回最大比赛的球队名称。任何线索?

id,winner,season  
1, Chelsea, 2014 
2, Chelsea, 2014 
3, Chelsea, 2015 
4, Arsenal, 2014

1 个答案:

答案 0 :(得分:1)

这个应该有效:

select t2.*
from (
select season, max(totalwins) as totalwins from (
select season, winner, count(*) as totalwins
  from matches t
  group by season, winner
) s1 group by season) w join
(select season, winner, count(*) as totalwins
  from matches t
  group by season, winner
) t2 on t2.season = w.season and t2.totalwins = w.totalwins;

结果:

season  winner  totalwins
------  ------- ---------
2014    Chelsea 2
2015    Arsenal 1
2015    Chelsea 1