我有一个销售表,需要计算遇到案例时的平均调用时间。我正在对该月的每一天进行查询。
我的查询是
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。如何编写正确的查询?
最佳答案
删除case
并将条件移至where
:
SELECT AVG(call_length) as avg_call_length
FROM SALES
WHERE date(call_date) = '2018-07-30'
AND outcome IN ('Sale1', 'Sale2')
请注意对条件进行编码的更简单方法。
关于MySQL:如何找到特定行的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643333/