简体   繁体   中英

Select Max() of SUM() for a Group

SELECT tname , pname , SUM(Goals)
FROM team as t , player as p , matches as m 
WHERE t.TID=p.TID AND p.PNB=m.PNB
Group BY pname , tname
ORDER BY `SUM(Goals)`  DESC

I need to Select only the player who have more goals in each team.Example it will shown only Luis Suarez from FC Barcelona and Munir fROM SEVILLA ....

我只需要选择在每支球队都有更多进球的球员。例如,它将只显示来自 F.C Barcelona 的 Luis Suarez 和来自 SEVILLA 的 Munir ....

Use proper, explicit, standard JOIN syntax. Commas were superseded over 20 years ago.

Then, you want window functions:

SELECT tp.*
FROM (SELECT t.tname, p.pname, SUM(m.Goals) as goals,
             RANK() OVER (PARTITION BY t.tname ORDER BY SUM(m.Goals) DESC) as seqnum
      FROM matches m JOIN
           team t
           ON t.TID = p.TID JOIN
           player p  
           ON p.PNB = m.PNB
      GROUP BY p.pname, t.tname
     ) tp
WHERE seqnum = 1
ORDER BY goals DESC;

This returns all players with the highest values. If you want just one row, use ROW_NUMBER() instead of RANK() .

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