[英]Recursively calculate records within a time interval in mysql
I have a table where records will be getting inserted every 4 hours on a daily basis.我有一张表,每天每 4 小时将插入一次记录。 If the record was not inserted for continuous 4 hours, I need to insert a log into another table.
如果连续 4 小时没有插入记录,我需要在另一个表中插入一条日志。 Below is the table schema.
下面是表模式。
Id DocPathid CreatedAt
1 1 2021-04-02 00:00:00
2 1 2021-04-02 04:00:00
3 1 2021-04-02 09:00:00
4 1 2021-04-02 12:00:00
5 1 2021-04-02 16:00:00
6 1 2021-04-02 20:00:00
7 1 2021-04-02 24:00:00
In the above case, there was no records inserted within a interval of 4hours (ie between 2021-04-02 04:00:00
& 2021-04-02 09:00:00
).在上述情况下,在 4 小时的间隔内(即
2021-04-02 04:00:00
和2021-04-02 09:00:00
之间)没有插入记录。 The query should return no.查询应该返回 no。 of failure count (in this case it is failed for 1 time).
失败计数(在这种情况下失败了 1 次)。
Is there a way to achieve this in MySQL?有没有办法在 MySQL 中实现这一点?
You can do something like this.你可以做这样的事情。
select count(1)
from (
select id, CreatedAt , timestampdiff(hour, CreatedAt
, lead(CreatedAt,1) over (partition by DocPathid order by CreatedAt) ) as hour
from Table1
) t
where hour >4
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9b0c631145422dbccd2ea23f0a7d2011 https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9b0c631145422dbccd2ea23f0a7d2011
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.