0

I am using the following results table

Results Table
__________________________________________
player_a| player_b | player_c |   year 
________|________  |__________|___________|
100      150        150        2015  
100      -50        -50        2015 
100      350        250        2014
200      350        250        2014  

What i would like to do is get the sum for each player per year. Using the following Select i get the desired results.

SELECT (

SELECT SUM( player_a )
FROM `results`
WHERE year =2015
) AS player_a_2015, (

SELECT SUM( player_a)
FROM `results`
WHERE year =2014
) AS player_a_2014

How can i get the maximum sum result only ?

ie For player_a should be 300

3 Answers 3

1
SELECT SUM(player_a) AS spa
FROM results
GROUP BY year
ORDER BY spa DESC
LIMIT 1
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, that's exactly what i was looking for!
0

You may wanna use,

SELECT MAX(SUM_A),YEAR FROM (
    SELECT SUM( player_a ) as SUM_A, YEAR
    FROM results
GROUP BY YEAR
) A_SCORE;

This will do it in one go(obviously for one player).

Comments

0

This should do it.

Make sure you wrap your OR condition in parenthesis so it evaluates them both.

SELECT SUM(player_a) AS sum_score
  FROM results
 WHERE (year = '2015' OR year = '2014')

EDIT

If you're wanting the maximum score by year, and then to sum that - this should be OK.

SELECT MAX(max_sum_score) AS max_sum_score
  FROM ( SELECT year
              , SUM(player_a) AS max_sum_score
           FROM results
          WHERE (year = '2015' OR year = '2014')
          GROUP BY year 
       ) a

enter image description here

enter image description here

4 Comments

Actually, looking at it - how would it come to 300? The answer I posted would get you 500 - are you wanting the Maximum score for each year?
maybe i didnt explain it correctly. i would like to get the highest value, from the 2 sums, not the sum of those 2
Just setting up a table in MySQL to test that SQL for you... 1 sec
Yep - the edited SQL returns 300 in the table I have setup to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.