MySQL:如何找到特定行的平均值?
我有一个销售表,需要计算满足案例时的平均通话时间。我每天都在做这个查询 我的问题是MySQL:如何找到特定行的平均值?,mysql,Mysql,我有一个销售表,需要计算满足案例时的平均通话时间。我每天都在做这个查询 我的问题是 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条记录
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条记录,那么平均调用长度除以100,而不是Sale1或Sale2的记录数。如何编写正确的查询?删除
案例
,并将条件移动到位置
:
SELECT AVG(call_length) as avg_call_length
FROM SALES
WHERE date(call_date) = '2018-07-30'
AND outcome IN ('Sale1', 'Sale2')
请注意编码条件的简单方法。请发布您的模式、示例数据和预期结果。