Calculate the general average grade with sum group by

久未见 提交于 2020-12-26 11:11:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!