1

I'm trying to find the average score received by films between a certain time

SELECT SUM (score * ?) / SUM(?) AS AGGSCO
  FROM movie, casting
       INNER JOIN casting
 WHERE movie.id = casting.movieid
       AND yr BETWEEN 1930 AND 1939;

Instead of the question marks, I think I would need the number of occurrences. Following this I would need to repeat the code to find the average between 40 and 49 and then 50 to 59. Is there a loop that I can use or would I need to rewrite the code? Thanks for any advice!

ps: I'm using squirrel, not mysql so the syntax may differ

Alexander
  • 3,129
  • 2
  • 19
  • 33

2 Answers2

1
select sum(score) / count(*) as AverageScote
from ...

or even better

select avg(score)
from ...
Z .
  • 12,657
  • 1
  • 31
  • 56
0

From the top of my head:

SELECT avg(score) AS AGGSCO, truncate(yr/10,0) AS DECADE
  FROM movie, casting
       INNER JOIN casting
 WHERE movie.id = casting.movieid
 GROUP BY truncate(yr/10,0)

See also this question.

Community
  • 1
  • 1
david a.
  • 5,283
  • 22
  • 24