我有一个销售表,需要计算满足案例时的平均调用时间。我每个月的每一天都在执行查询。
我的问题是
SELECT AVG(case when (outcome= 'Sale1' or outcome='Sale2') then call_length else 0 end) as avg_call_length
FROM SALES
WHERE year(call_date)='2018' and month(call_date)='7' and day(call_date)='30'
假设我有100条记录,那么avg_call_length除以100,而不是有多少条记录是Sale1或Sale2。如何编写正确的查询?
发布于 2018-08-02 07:41:31
删除case
并将条件移动到where
SELECT AVG(call_length) as avg_call_length
FROM SALES
WHERE date(call_date) = '2018-07-30'
AND outcome IN ('Sale1', 'Sale2')
请注意更简单的条件编码方法。
https://stackoverflow.com/questions/51643333
复制相似问题