SQL: Finding Average Score

微笑、不失礼 提交于 2019-12-23 04:37:27

问题


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:

  1. 3schools
  2. SQL Server - MSDN


来源:https://stackoverflow.com/questions/17098020/sql-finding-average-score

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