Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:如何找到特定行的平均值?_Mysql - Fatal编程技术网

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')

请注意编码条件的简单方法。

请发布您的模式、示例数据和预期结果。