【问题标题】:Calculate average of entries in same table mysql计算同一张表mysql中条目的平均值
【发布时间】:2017-07-19 11:32:09
【问题描述】:

我在一个表格中有一堆条目,如下所示

|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

我想获取 Lead added to Verified 中所有条目的平均值

非常感谢任何帮助

【问题讨论】:

  • 您错过了时间戳列名称。我已编辑您的问题以添加它。
  • 谢谢,名字是ts,我以为我把它包括在内了。感谢您的帮助!
  • 当 new_status 变为已验证时,数据会是什么样子?
  • 如上图。实际上,正在发生的事情是在添加潜在客户时输入一个条目(行 id 968),然后当潜在客户状态从已添加更改为已验证(行 id 4275)时添加另一个条目,其中有很多唯一值是contact_id和机构_id

标签: mysql diff average datediff


【解决方案1】:

如果我理解正确,您希望 leadAddedstatusChange 之间的区别为“引用”。这表明条件聚合或连接:

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

【讨论】:

  • 我得到空结果,直到我删除状态已更改现在我得到这个,是几秒钟吗? mysql> select avg(datediff(tq.ts, tv.ts)) as avg_time_to_quote -> from lead_history tv join -> lead_history tq -> on tv.agency_id = tq.agency_id 和 -> tv.alter_type = 'Lead added' 和-> tq.new_status = '引用'; +-------------------+ | avg_time_to_quote | +-------------------+ | 58.4144 | +-------------------+
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2022-12-02
相关资源
最近更新 更多