计算相同 table mysql 中条目的平均值

Calculate average of entries in same table mysql

我在 table 中有一堆条目,如下所示

|id  | agency_id|old_status|new_statusts|timestamp         |contact_id|alter_type  |last_mod_by|
------------------------------------------------------------------------------------------------
|968 |4926185635|  Verified|  Verified  |2016-11-2210:46:56|       969|LeadAdded   |          1|
|4274|4926185635|  Verified|  Verified  |2017-01-1410:46:56|       969|NoteAdded   |          1|
|4275|4926185635|  Verified|  Quoted    |2017-01-2110:46:56|       969|StatusChange|          1|

我想做的是获取从添加时间到验证时间以及验证到引用时间的累计平均时间。

我浏览了本网站上的不同条目,似乎找不到合适的内容。

实际上发生的事情是在添加潜在客户(行 ID 968)时放入一个条目,然后在潜在客户状态从已添加更改为已验证(行 ID 4275)时添加另一个条目,其中有很多唯一值是 contact_id 和 agency_id

我想获取已添加到已验证的潜在客户的所有条目的平均值

非常感谢任何帮助

如果我没理解错的话,你想要leadAddedstatusChange之间的区别是"Quoted"。这表明条件聚合或连接:

select avg(datediff(tq.ts, tv.ts)) as avg_time_to_quote
from t tv join
     t tq
     on tv.agency_id = tq.agency_id and
        tv.alter_type = 'LeadAdded' and
        tq.alter_type = 'StatusChange' and
        tq.new_status = 'Quoted';