问题

Given the table, I am looking for a query to calculate the average score of a given question for a given teacher.
If I was going to add another column to the table like SCHOOLID and wanted to have a column that calculated the average for a given question for that school, how would I do that?
Thanks in advance for all of your help!
回答1:
select teacherId, questionid, AVG(Score)
From myTable
group by teacherId, questionid
回答2:
You can create a stored procedure to handle the whole requirements
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
回答3:
You can do this:
SELECT TeacherId, QuestionId, AVG(Score)
FROM TABLE-NAME
GROUP BY TeacherId, QuestionId
Here are a few links that will help:
- 3schools
- SQL Server - MSDN
来源:https://stackoverflow.com/questions/17098020/sql-finding-average-score