This is part of my table on MySQL database
+----------+---------------------+--------+
| sID | sDatetime | sETX |
+----------+---------------------+--------+
| 16213404 | 2020-04-24 16:00:00 | 497681 |
| 16213398 | 2020-04-20 14:58:56 | 281011 |
+----------+---------------------+--------+
This table count with 14.121.398 records
I realized that in this case more than one hour has passed between the previous and the next row
mysql> SELECT
TIMEDIFF(
'2020-04-20 16:00:00',
'2020-04-20 14:58:56'
);
+------------------------------------------------------------------+
| TIMEDIFF(
'2020-04-20 16:00:00',
'2020-04-20 14:58:56'
) |
+------------------------------------------------------------------+
| 01:01:04 |
+------------------------------------------------------------------+
1 row in set
this is not possible because the data is downloaded maximum from the source every five minutes
in this case is missing the time slot between 3pm and 4pm
I have tried this query without success because the return is all zero
I think because the sID is not consecutive
The code I've tried below
SELECT A.`sID`, A.`sDatetime`, (B.`sDatetime` - A.`sDatetime`) AS timedifference
FROM tbl_2020 A INNER JOIN tbl_2020 B ON B.sID = (A.sID + 1)
ORDER BY A.sID ASC;
how can i find this anomaly in mysql table?
my version of MySQL is 5.5.62-log
the name of column is sDatetime the type is Datetime.
any suggestion, please?
thanks in advance for any help
edit #01
+----------+-----------+---------------------+
| sID | time_diff | sDatetime |
+----------+-----------+---------------------+
| 18389322 | 301 | 2020-05-16 23:53:29 |
| 18390472 | 308 | 2020-05-16 23:48:21 |
| 18389544 | 301 | 2020-05-16 23:43:20 |
| 18388687 | 303 | 2020-05-16 23:38:17 |
| 18388398 | 301 | 2020-05-16 23:33:16 |
| 18390451 | 308 | 2020-05-16 23:28:08 |
| 18388915 | 302 | 2020-05-16 23:23:06 |
| 18388208 | 301 | 2020-05-16 23:18:05 |
| 18390516 | 301 | 2020-05-16 23:13:04 |
| 18389904 | 301 | 2020-05-16 23:08:03 |
+----------+-----------+---------------------+
mysql> SELECT
TIMEDIFF(
'2020-05-16 23:53:29',
'2020-05-16 23:48:21'
) AS td;
+----------+
| td |
+----------+
| 00:05:08 |
+----------+
1 row in set
You should try something like this
SELECT
sID
,TIME_TO_SEC(TIMEDIFF(@date,sDatetime)) time_diff
,@date := sDatetime
,sETX
FROM(
SELECT * FROM table1
ORDER BY sDatetime DESC) s1,(SELECT @date :=(SELECT MAX(sDatetime) FROM table1)) s2
HAVING time_diff > 300
First you order the table by time, then you get the time difference between two consecutive rows and check if they are bigger than 5 minutes
see example here https://www.db-fiddle.com/f/2yKt6d5RWngXVYJKPGZL6m/8
Comparing current row to previous works
drop table if exists t;
create table t
(sID int, sDatetime datetime, sETX int);
insert into t values
( 16213404 , '2020-04-24 16:00:00' , 497681),
( 16213398 , '2020-04-20 14:58:56' , 281011);
select sid,sdatetime,(select sdatetime from t t1 where t1.sid < t.sid order by t1.sid desc limit 1) prevdt,
time_to_sec(sdatetime) - time_to_sec((select sdatetime from t t1 where t1.sid < t.sid order by t1.sid desc limit 1)) diff
from t
where time_to_sec(sdatetime) - time_to_sec((select sdatetime from t t1 where t1.sid < t.sid order by t1.sid desc limit 1)) > 300;
+----------+---------------------+---------------------+------+
| sid | sdatetime | prevdt | diff |
+----------+---------------------+---------------------+------+
| 16213404 | 2020-04-24 16:00:00 | 2020-04-20 14:58:56 | 3664 |
+----------+---------------------+---------------------+------+
1 row in set (0.002 sec)
If this is too slow add your table definition so that we can see the indexes you have.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.