MySQL Forums
Forum List  »  Newbie

Re: tutorial incorrect calculation want to understand
Posted by: Phillip Ward
Date: January 28, 2015 06:37AM

Your "select" and your "group by" don't match.

SELECT 
  s.yearID 
, s.teamID 
, SUM(s.salary) 
, t.W  
, SALARY / t.W 
. . . 
GROUP BY s.teamID

Anything that you select that isn't in an aggregate function (sum, count, etc.) must be included as a "level" within your grouping. If you don't, then you get all sorts of odd results, just like this, because MySQl doesn't know how to roll things up - you haven't told it.

Try something more like this:
SELECT 
  s.teamID 
, SUM( s.salary ) AS SALARY 
, SUM( t.W ) AS WINS 
, SUM( s.salary / t.W ) AS CPW 

from       salaries  s 
inner join teams  t 
      on   s.teamID = t.teamID 
      and  s.yearID = t.yearID 

where s.yearID = 2002 

group by s.teamID 
order by salary desc 
; 

Options: ReplyQuote


Subject
Written By
Posted
Re: tutorial incorrect calculation want to understand
January 28, 2015 06:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.