从MAX获取ID(SUM())SQL GROUP BY

时间:2016-04-17 17:25:50

标签: sql oracle

我试图从我在下面的图片

中获得的结果中获取PlayerID

Result of query

我用来显示的查询是:

SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde 
FROM PlayerPerformance GROUP BY PlayerIDFK;

我还有另一个查询返回这个案例中两列总和的最大值47,这是正确的答案,但我也想获得PlayerIDFK。

SELECT MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;

当我尝试使用查询获取玩家ID时,我得不到单组组功能。这是我试图使用的查询:

SELECT PlayerIDFK, MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;

1 个答案:

答案 0 :(得分:0)

使用order byrownum(或12c +中的fetch first 1 row only):

SELECT *
FROM (SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde 
      FROM PlayerPerformance
      GROUP BY PlayerIDFK
      ORDER BY ( sum(TwoPointMade) + sum(ThreePointMade) ) desc
     ) pp
WHERE rownum = 1;