SQL Average计算基于其他列的条件

时间:2017-06-29 21:26:04

标签: sql where

如何根据其他列的条件输出一列的平均值?例如,这就是我所做的:

SELECT  
    AVG(TestScores WHERE ID = 1 AND Date_On BETWEEN '2017-04-08' AND '2017-04-12' AND Code = '87A')    
FROM
    Table_Name

我希望的目标是在TestScores ID的{​​{1}}以及日期在指定的时间间隔之间获得row = 1列的平均值。这可能吗?

2 个答案:

答案 0 :(得分:1)

如果您使用的是Sql Server,那么下面就是您要查找的内容(我认为):

SELECT 
    AVG(TestScores)   
FROM
    Table_Name
WHERE ID = 1 
    AND Date_On BETWEEN '2017-04-08' AND '2017-04-12' 
    AND Code = '87A'
  

编辑,根据下面的评论:

SELECT 
    ID,
    AVG(TestScores)   
FROM
    Table_Name
WHERE 
    Date_On BETWEEN '2017-04-08' AND '2017-04-12' 
    AND Code = '87A'
GROUP BY ID

答案 1 :(得分:0)

这是查询: -

SELECT  AVG(TestScores)    
FROM  Table_Name 
WHERE ID = 1 AND
Date_On BETWEEN '2017-04-08' AND '2017-04-12' AND Code = '87A'