对于一个项目,我必须计算不同类型项目的 AVG 间隔。
示例数据
|source|name|type|date |time |
|ABC |AP1 |AP |02/01/2020|11:45:23|
|ABC |AP1 |AP |02/01/2020|12:50:26|
|ABC |AP1 |AP |02/01/2020|14:15:23|
|ABC |AP2 |PI |02/01/2020|15:21:12|
|ABC |AP2 |PI |02/01/2020|16:22:12|
|ABC |AP2 |OK |02/01/2020|16:04:21|
我需要做的是计算每个来源、名称、类型组合过去 6 个月的平均间隔。
我在 SQL 中尝试了多种不同的结果,但到目前为止,它只给了我更新的最新时间,并且大多数在线解决方案都无法帮助我。我希望有人有想法或提示,以便我可以继续。
到目前为止的代码
select Source, name, Type, count(time)
from myTable
where time>= DATEADD(month, -6, getdate())
group by Source, name, Type,
编辑
预期结果应显示平均间隔,因此类似于
|source|name|type|date |time |avg interval|
|ABC |AP1 |AP |02/01/2020|11:45:23|00:45:00 |
|ABC |AP1 |AP |02/01/2020|12:50:26|00:45:00 |
|ABC |AP1 |AP |02/01/2020|14:15:23|00:45:00 |
|ABC |AP2 |PI |02/01/2020|15:21:12|01:01:00 |
|ABC |AP2 |PI |02/01/2020|16:22:12|01:01:00 |
|ABC |AP2 |OK |02/01/2020|16:04:21|24:00:00 |
我不确定你想要什么结果。但以下仅使用过去六个月的数据并计算有多于一行的平均空间:
select Source, name, Type,
datediff(second, min(time), max(time)) / nullif(count(*) - 1, 0)
from myTable
where time>= DATEADD(month, -6, getdate())
group by Source, name, Type;
注意:这不包括每个来源的最新行之后的任何时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句