5

I have a lot of teams and i want to know the maximum of sum total of each team.

This's my query :

SELECT campaign_id, 
       campaign_identifier, 
       team, 
       campaign_name, 
       Month(time) AS month, 
       Sum (total) AS Total 
FROM   campaign 
WHERE  Year(time) = Year(Now()) 
       AND Month(time) = 12 
GROUP  BY campaign_identi, 
          team 

Thanks.

2 Answers 2

7

If am not wrong this is what you need

select team,max(Total)
from
(
SELECT team, 
       Sum (total) AS Total 
FROM   campaign 
WHERE  Year(time) = Year(Now()) 
       AND Month(time) = 12 
GROUP  BY campaign_identi, 
          team 
) A
Group by team
0
2
SELECT MAX(Total) AS max_sum_total
    FROM (
        SELECT ... -- your query
         ) AS x;

You did not ask for it, but you probably want to say which team had the max? Then you need groupwise max.

3
  • Thanks for your answer, but i want to return a max of sum of each team not which team had the max Dec 20, 2015 at 23:50
  • 2
    Doesn't your query do that?
    – Rick James
    Dec 21, 2015 at 0:20
  • 2
    The OP's current query returns SUM(Total) per team and campaign, and the OP seems to want the MAX(SUM(...)) per team.
    – Andriy M
    Dec 21, 2015 at 8:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.