我可以根据一个查询中另一列的值得到mySQL数据库中某一列的平均值吗?

我可以根据一个查询中另一列的值得到mySQL数据库中某一列的平均值吗?,mysql,sql,count,pivot,average,Mysql,Sql,Count,Pivot,Average,我有一张客户电话活动表。在表中,我有一列表示调用的长度(以秒为单位),另一列表示“首次调用”(true/false)。我希望找到一种方法,将首次通话的平均通话时间与非首次通话的平均通话时间分开?这在单个mySQL查询中可行吗 SELECT location, count(*) AS total, sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall, sum(case when answered

我有一张客户电话活动表。在表中,我有一列表示调用的长度(以秒为单位),另一列表示“首次调用”(true/false)。我希望找到一种方法,将首次通话的平均通话时间与非首次通话的平均通话时间分开?这在单个mySQL查询中可行吗

SELECT location,
    count(*) AS total,
    sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall,
    sum(case when answered = 'Yes' then 1 else 0 end) AS answered,
    sum(case when tags like '%Lead%' then 1 else 0 end) as lead,
    sum(case when tags like '%arbage%' then 1 else 0 end) as garbage,    
    avg(case when duration........firstTime = True???)
FROM staging
GROUP BY location

我想这样说:

select 
    location,
    count(*) as total,
    sum(firstcall = 'true'  ) as cnt_firstcall,
    sum(answered = 'Yes'    ) as cnt_answered,
    sum(tags like '%Lead%'  ) as cnt_lead,
    sum(tags like '%arbage%') as cnt_garbage,    
    avg(case when firstcall = 'true'  then duration end) as avg_first_call,_duration
    avg(case when firstcall = 'false' then duration end) as avg_non_first_call_duration
from staging
group by location
理由:

  • MySQL将真/假条件解释为数字上下文中的1/0值,这大大缩短了条件
    sum()

  • avg()


选择位置、首次呼叫、平均值(持续时间)。。。按地点分组,firstCall
'end'在声明中被损坏时,我更新了答案,非常感谢。是否可以将这些值四舍五入,这样就没有小数了?比如211.7895就是211?谢谢你的跟进。我对堆栈溢出有点陌生,不知道如何将“答案”归因于一个问题。我之前曾将对方标记为已回答,但如果你的答案更好,我会更改该属性吗?@TomHall:你可以自由接受你最喜欢的答案,你可以随意更改。请看:@TomHall:也相关:@TomHall:您的表情看起来不错。
select 
    location,
    count(*) as total,
    sum(firstcall = 'true'  ) as cnt_firstcall,
    sum(answered = 'Yes'    ) as cnt_answered,
    sum(tags like '%Lead%'  ) as cnt_lead,
    sum(tags like '%arbage%') as cnt_garbage,    
    avg(case when firstcall = 'true'  then duration end) as avg_first_call,_duration
    avg(case when firstcall = 'false' then duration end) as avg_non_first_call_duration
from staging
group by location