SQL帮助查找最大总值

时间:2011-03-20 23:06:24

标签: sql

我需要帮助根据此表找到球员和球队名称的总分数:

Tbl.STATS [Match_ID,Player_ID,Points_Scored,Team_Name]

非常感谢任何建议或帮助!

3 个答案:

答案 0 :(得分:2)

所有球队的所有球员

select player_id, team_name, sum(points_scored) sum_score
from stats
group by player_id, team_name
order by sum_score desc

球员和球队名称(例如,鲍勃为老鹰队效力,所以不考虑鲍勃为鲨鱼队效力的时候)

select sum(points_scored) sum_score
from stats
where player_id = 1 and team_name = 'Eagles'

但是这个问题再次提出了一些问题,所以问题就是MAX总分:

select max(sum_score) MaxTotalScore
from
(
    select player_id, team_name, sum(points_scored) sum_score
    from stats
    group by player_id, team_name
) X

答案 1 :(得分:0)

SELECT player_id,team_name,sum(points_scored)FROM STATS Group by player_id,team_name

答案 2 :(得分:0)

  

“我正在寻找那个玩家   得分最高的分数和什么   球员的队伍。“

我通常不会涉及Oracle标签,但我相信这应该有效!

WITH T AS
(
SELECT Player_ID, 
       Team_Name, 
       SUM(Points_Scored) OVER (PARTITION BY Player_ID) AS Points_Total
FROM Tbl.STATS 
), T2 AS
(
SELECT Player_ID, 
       Team_Name, 
       Points_Total,
       RANK() OVER (ORDER BY Points_Total DESC) AS RN
FROM T       
)
SELECT Player_ID, 
       Team_Name, 
       Points_Total
FROM T2 
WHERE RN=1