2

嗨,我正在尝试计算结果集的平均值,但我无法这样做,选择 count(*) 的查询返回 1 月 1 日至 31 日之间每天的调用计数,因此返回了近 20 行。我应该如何计算结果的平均值,然后将值分配给像 SET @abc = (下面的查询)这样的变量。我正在使用 sql server 2005。

SELECT avg(Calls) AS average 
FROM
(
     SELECT COUNT(*) Calls      
     FROM abc 
     WHERE CallDate  BETWEEN '20120101'  AND '20120131' 
     AND datepart(Hh,TranscribeDateTime) = '07' 
     AND datepart(Mi,TranscribeDateTime) BETWEEN '00' AND '59'  
     AND AppID = 123 AND Status ='T' 
     GROUP BY calldate
)

如果我的方法是错误的,请提出一个。谢谢你

4

1 回答 1

6

Looks like you are just missing a table alias for the derived table (required in SQL Server) and the assignment statement (also you can't alias the column AS average if you are assigning it to a variable)

DECLARE @ABC INT;

SELECT @ABC = AVG(Calls)
FROM
(
     SELECT COUNT(*) Calls      
     FROM abc 
     WHERE CallDate  BETWEEN '20120101'  AND '20120131' 
     AND datepart(Hh,TranscribeDateTime) = '07' 
     AND datepart(Mi,TranscribeDateTime) BETWEEN '00' AND '59'  
     AND AppID = 123 AND Status ='T' 
     GROUP BY calldate
) T  /* <-- Alias here */
于 2012-12-19T20:30:43.760 回答