鉴于该表,我正在寻找一个查询来计算给定教师的给定问题的平均分数。
如果我要在这个表格中添加另一个列,例如SCHOOLID,并希望有一个列来计算该学校给定问题的平均值,我该怎么做?
提前感谢您的所有帮助!
答案 0 :(得分:4)
select teacherId, questionid, AVG(Score)
From myTable
group by teacherId, questionid
答案 1 :(得分:1)
您可以创建存储过程来处理整个要求
CREATE PROCEDURE avarageCalc
(
@teacherid nvarchar(5) = null,
@questionid int = null,
@schoolid int = null
)
AS
Begin
SELECT AVG(SCORE) as Avarage
FROM Table
WHERE (TeacherID = @teacherid OR @teacherid IS NULL) AND
(QuestionId = @questionid OR @questionid IS NULL) AND
(SchoolId = @schoolid OR @schoolid IS NULL)
END
答案 2 :(得分:1)
你可以这样做:
SELECT TeacherId, QuestionId, AVG(Score)
FROM TABLE-NAME
GROUP BY TeacherId, QuestionId
以下是一些有用的链接: