如何汇总多个不同的值并在SQL中获取最大值

时间:2016-04-17 14:23:12

标签: sql oracle

我在Oracle SQL中使用distinct select,我想要做的是总结特定ID的所有数据。图片示例:

The ouput of query now

因此,例如,当PlayerIDFK为1时,我想在一列中总结TwoPointsMade而在另一列中总结ThrePointsMade,因此结果将是

PlayerIDFK       TwoPointsMade    ThreePointsMade
--------------------------------------------------
1                 5                  2

我现在使用的查询是:

SELECT PlayerIDFK, TwoPointMade, ThreePointMade
  FROM PlayerPerformance
  WHERE PlayerIDFK IN (SELECT DISTINCT PlayerIDFK
                         FROM PlayerPerformance);

2 个答案:

答案 0 :(得分:0)

我认为你可以使用groupby-clause,你可以通过playerIDFK进行分组并总结twopointsmade和threepointsmade

这样的事可能有用:

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

如果你想获得最大值,你可以做一个嵌套查询,如下所示:

SELECT MAX(inside_query.m1, inside_query.m2) FROM (SELECT PlayerIDFK, sum(TwoPointMade) as m1, sum(ThreePointMade)as m2 FROM PlayerPerformance GROUP BY PlayerIDFK) as inside_query

可能有一种更优雅的方式来做,但这就是我所得到的:)

答案 1 :(得分:0)

也许这可能会有所帮助:

SELECT * FROM ( SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade FROM PlayerPerformance GROUP BY PlayerIDFK ORDER BY sum(TwoPointMade) DESC ) WHERE ROWNUM = 1 UNION ALL SELECT * FROM ( SELECT PlayerIDFK, sum(ThreePointMade) as ThreePointMade FROM PlayerPerformance GROUP BY PlayerIDFK ORDER BY sum(ThreePointMade) DESC ) WHERE ROWNUM = 1