sql - 按 TermID 和 yearID 的 SQL 平均值
问题描述
这是我的 SQL 代码包含表:tblstudentdetail 和 tblstudentinfo
SELECT distinct TblstudentInfo.FirstName +''+ TblstudentInfo.LastName as Name,TblstudentInfo.StudentID, TblstudentInfo.YearID, TblstudentInfo.DualCredits, TblstudentInfo.DC_Classes, TblstudentInfo.DC_GPA, tblStudentDetail.StatusID, TblstudentInfo.ActScore,tblStudentDetail.YearID
,tblStudentDetail.TermID,tblStudentDetail.GPA
FROM TblstudentInfo
left join tblStudentDetail on TblstudentInfo.StudentID = tblStudentDetail.StudentID
order by name
结果:
Name StudentID YearID DualCredits DC_Classes DC_GPA StatusID ActScore YearID TermID GPA
Aaron 124 9 NULL NULL NULL 1 17 10 1 2.901
Aaron 124 9 NULL NULL NULL 1 17 10 2 3.171
Aaron 124 9 NULL NULL NULL 1 17 11 1 2.31
Aaron 124 9 NULL NULL NULL 1 17 11 2 3.31
Debbie 2344 7 NULL NULL NULL 1 19 9 1 4.31
Debbie 2344 7 NULL NULL NULL 1 19 10 1 4.31
我如何平均平均 GPA?我想要这样的结果:
Aaron 124 9 NULL NULL NULL 1 17 2.923
Debbie 2344 7 NULL NULL NULL 1 19 4.31
解决方案
只需在 GPA 上使用 AVG() 函数,然后按其余列分组
SELECT si.FirstName +''+ si.LastName as Name, si.StudentID,
si.YearID, si.DualCredits, si.DC_Classes,
si.DC_GPA, si.StatusID, si.ActScore, si.YearID,AVG(d.GPA)
FROM TblstudentInfo si LEFT JOIN tblStudentDetail d
ON si.StudentID = d.StudentID
GROUP BY si.FirstName, si.LastName, si.StudentID,si.YearID, si.DualCredits, si.DC_Classes, si.DC_GPA, si.StatusID, si.ActScore, si.YearID;
推荐阅读
- c - c 数组中不完整的元素类型
- python - 尝试使用 Google 搜索搜索图像,错误 400
- java - 在 Postman 中使用 REST 控制器测试 Spring 应用
- wordpress - Git没有将文件夹添加到repo
- botframework - 使用 bot 框架 SDK4.0 的 bot 项目中 BotConfiguration.bot 的结构是什么以及如何将其添加到项目中?
- postgresql - “GRANT SELECT table TO 角色”不起作用
- c++ - 为什么 ulimit -v 在 clang 的地址清理程序下不起作用?
- algorithm - 如何用八个 '8' 和 +、-、*、/ 计算 1000
- android - Java 类和 Xml 文件中的代码错误
- vuejs2 - Vue 2 中未定义的道具