比较行并选择最大值

时间:2016-04-06 09:16:55

标签: mysql sql foreach max

我有下表:

FileName | SubFileName | TotalPlayersCount |
-------------------------------------------
AAA      |     SF1     |        11         |     
AAA      |     SF2     |        5          |    
AAA      |     SF3     |        3          |  
BBB      |     SF1     |        8          |    
BBB      |     SF2     |        15         |   
BBB      |     SF3     |        2          | 
CCC      |     SF1     |        5          |      
CCC      |     SF2     |        10         |    
CCC      |     SF3     |        20         | 

如您所见,每个FileName都有3个不同的SubfileName('SF1','SF2','SF3')。

这些SubfileName中的每一个都有TotalPlayersCount的不同值。

我正在尝试从三个TotalPlayersCount中选择列SubfileName的最大值,并且 FOREACH FileName

结果应为:

FileName | SubFileName | TotalPlayersCount |
-------------------------------------------
AAA      |     SF1     |        11         | 
BBB      |     SF2     |        15         | 
CCC      |     SF3     |        20         | 

我尝试了几个问题,这是我最接近的问题:

select distinct FileName, max(TotalPlayersCount) AS TotalPlayersCount
from dbo.MyTestTable
group by FileName

这是我得到的结果:

FileName | TotalPlayersCount |
------------------------------
AAA      |        11         | 
BBB      |        15         | 
CCC      |        20         | 

所以现在我错过了结果中的SubfileName

你能帮我找到遗失的东西吗?

提前致谢。

4 个答案:

答案 0 :(得分:2)

简单的方法,如果没有其他行具有相同的FileName具有更高的TotalPlayersCount,则使用NOT EXISTS返回一行:

select FileName, SubFileName, TotalPlayersCount 
from dbo.MyTestTable t1
where not exists (select 1 from dbo.MyTestTable t2
                  where t2.FileName = t1.FileName
                    and t2.TotalPlayersCount > t1.TotalPlayersCount)

JOIN版本,也许是更好的表现:

select t1.FileName, t1.SubFileName, t1.TotalPlayersCount 
from dbo.MyTestTable t1
JOIN (select FileName, max(TotalPlayersCount) as TotalPlayersCount
      from dbo.MyTestTable
      group by FileName) t2
  ON  t2.FileName = t1.FileName
  and t2.TotalPlayersCount = t1.TotalPlayersCount

如果出现平局,两个查询都将返回两行!

答案 1 :(得分:0)

select fileName,subFileName,max(TotalPlayersCount )
from table_name 
group by filename

答案 2 :(得分:0)

尝试此查询:

SELECT a.FileName, a.SubFileName, a.TotalPlayersCount
FROM MyTestTable a INNER JOIN (SELECT FileName, max(TotalPlayersCount) AS TPC
                               FROM MyTestTable
                               GROUP BY FileName
                              ) b ON a.FileName = b.FileName AND
                                     a.TotalPlayersCount = b.TPC
ORDER BY a.FileName, a.SubFileName

子查询返回您已经拥有的结果(不需要像{jarlh提到的DISTINCT)。结果将再次与表格结合以选择SubFileName

请注意,如果您有多个SubFileName,其最大值为TotalPlayersCount,则会选择第一个SubFileName,因为它是ORDER BY的一部分。

答案 3 :(得分:0)

这适用于SQL Server - 也应该在MySQL中运行。

SELECT  m.FileName, MIN(s.SubFileName) AS SubFileName, m.TotalPlayersCount
FROM    SOURCE_TABLE s
        INNER JOIN 
            (
            SELECT  FileName, MAX(TotalPlayersCount) AS TotalPlayersCount
            FROM    SOURCE_TABLE
            GROUP BY FileName ) m ON m.FileName = s.FileName AND
                 m.TotalPlayersCount = s.TotalPlayersCount
GROUP BY m.FileName, m.TotalPlayersCount