I have a table that in one column saved grade of a course and in another column save the ratio of that course, then I want to calculate average of courses with ratio, I write these code but I faces with error, please help me
select SUM((grade * ratio) / SUM(ratio)) as averageOfCourses
from myTable
Your problem is probably misplaced parentheses.
If you truly want to do a SUM of the values divided by a SUM, then you have to work a good deal harder; you can’t do aggregates of aggregates directly.
To do what you wrote, you’d have to rewrite it as:
(The final
ASis required by standard SQL, but doesn’t contribute anything to this query.)In this context, I’m not even sure it gives a different answer.