2016-06-01 15 views
0

以下は私のSQLクエリです...私の平均は合計を平均化するのではなく、SUMを表示するだけです。私はなぜそう思うか分からない。誰でも洞察力を提供できますか?SQL平均が期待どおりに機能しない

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 
+1

あなたは 't1'が単一の数字を表すようにクエリを書いています。 't2'、' t3'などのように。 –

答えて

0

あなたはサブクエリでカウントを行うことで、暗黙的なGROUPを持っていると、あなたAVG上の単一の値(カウント)は、とても基本的に、あなたのクエリにAVGsがno-OPSません。

捨て外部クエリとサブクエリでAVGs代わりの数を計算します。

COUNT(CASE VAL THEN ... BETWEEN 1 END)を

AVG(CASE WHENになるべき... THEN VAL END BETWEEN VAL)

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の使用に注意してください。

+0

数字が正しく出てこないようです。 理想的には私はAVG(SUM(things to sum))をする必要がありますが、これは許されません。だから私は最初に外部の問い合わせをしたのです –

+0

@RobertLouisEverhardII。 。 。なぜ私は 'SUM()'が必要なのか分かりません。おそらくサンプルデータと望ましい結果がその質問に役立つだろう。 –

関連する問題