SQL平均值不像我预期的那样工作

时间:2016-06-01 22:22:30

标签: sql sql-server aggregate aggregates

下面是我的SQL查询...但是我的平均值并不是对总数进行平均,而只是显示SUM。我不知道为什么。任何人都可以提供任何见解吗?

   SELECT
AVG(a.t1) as '8:00-9:00',
AVG(a.t2) as '9:00-10:00',
AVG(a.t3) as '10:00-11:00',
AVG(a.t4) as '11:00-12:00',
AVG(a.t5) as '12:00-1:00',
AVG(a.t6) as '1:00-2:00',
AVG(a.t7) as '2:00-3:00',
AVG(a.t8) as '3:00-4:00',
AVG(a.t9) as '4:00-5:00',
AVG(a.t10) '5:00-6:00',
AVG(a.t11) as '6:00-7:00',
AVG(a.t12) as '7:00-8:00' 
from 
(select COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('07:00:00' AS time) AND CAST('08:00:00' AS time) then 1 end) as t1,
 COUNT(Case when CAST(Request_Datetime AS time)BETWEEN CAST('08:00:00' AS time) AND CAST('09:00:00' AS time) then 1 end) as t2,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('10:00:00' AS time) AND CAST('11:00:00' AS time) then 1 end) as t3,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('11:00:00' AS time) AND CAST('12:00:00' AS time) then 1 end) as t4,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('12:00:00' AS time) AND CAST('13:00:00' AS time) then 1 end) as t5,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('13:00:00' AS time) AND CAST('14:00:00' AS time) then 1 end) as t6,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('14:00:00' AS time) AND CAST('15:00:00' AS time) then 1 end) as t7,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('15:00:00' AS time) AND CAST('16:00:00' AS time) then 1 end) as t8,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('16:00:00' AS time) AND CAST('17:00:00' AS time) then 1 end) as t9,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('17:00:00' AS time) AND CAST('18:00:00' AS time) then 1 end) as t10,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('18:00:00' AS time) AND CAST('19:00:00' AS time) then 1 end) as t11,
 COUNT(Case when CAST(Request_Datetime AS time) BETWEEN CAST('19:00:00' AS time) AND CAST('20:00:00' AS time) then 1 end) as t12 ,
 COUNT(Interaction_ID) as dayCount
 from rt_queue_delta 
 Where DateName( dw,Request_datetime) in('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') and  
 CAST(Request_datetime as Date) >= '05/01/2015' and Department = 1) as a 

2 个答案:

答案 0 :(得分:0)

你有一个隐含的GROUP BY在子查询中执行COUNT,然后你对单个值(COUNTs)进行AVG,所以基本上在你的查询中AVG是无操作。

丢弃外部查询并在子查询中计算AVG而不是COUNT:

COUNT(在VAL之间的情况......那么1结束)

应该成为

AVG(在VAL之间的情况......那么结束了)

答案 1 :(得分:0)

我怀疑你想要的查询是:

select AVG(Case when CAST(Request_Datetime AS time) BETWEEN CAST('07:00:00' AS time) AND CAST('08:00:00' AS time) then 1.0 else 0 end) as t1,
       . . .
       AVG(Case when CAST(Request_Datetime AS time) BETWEEN CAST('19:00:00' AS time) AND CAST('20:00:00' AS time) then 1.0 else 0 end) as t12 ,
       COUNT(Interaction_ID) as dayCount
 from rt_queue_delta 
 Where DateName(dw, Request_datetime) in ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') and  
       CAST(Request_datetime as Date) >= '2015-05-01' and
       Department = 1;

这将返回每个时间段内总数的比例。请注意使用1.0else 0