问题
I have to display for each user the first and last name, year as studyYear, age, average grade, and also the general average grade.
General average grade I need to be computed like this:
Sum(AverageGrade)/ total number of grades
[userID]
[FirstName]
[LastName]
[BirthDate]
[Year]
[AverageGrade]
My problem is the sum of the the general average grade. I understand that I have to use Sum(AverageGrade)
+ group by
but it wont work, help?
select FirstName+' '+LastName as FullName,
AverageGrade ,Year as StudyYear,
DATEDIFF(year,BirthDate,GETDATE()) AS AgeInYears
from Student
How do I include the Sum(AverageGrade)
here? Or how do I rewrite?
FirstName LastName Year AverageGrade [GeneralAverageGrade]
Fatima Alo 2 9.20 7.45
Omar Kutum 1 5.88 7.45
Sado Kerkzm 3 7.20 7.45
回答1:
Do you just want a grand average of column averagegrade
over the whole dataset? If so, use window functions:
select s.*, avg(averagegrade) over() avg_averagegrade
from students s
来源:https://stackoverflow.com/questions/64671985/calculate-the-general-average-grade-with-sum-group-by