This is my MySQL table.
+-----------+-------------+---------------------+
| element | status | hour |
+-----------+-------------+---------------------+
| 18 | Available | 2020-01-19 14:23:49 |
| 18 | Unavailable | 2019-09-13 18:19:47 |
| 18 | Available | 2019-09-13 18:18:49 |
| 18 | Unavailable | 2019-09-09 08:22:45 |
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+-----------+-------------+---------------------+
Normally the timeline of rows in this MySQL table for each element status
is unavailable / available.
But it happened that for element number 19 the timeline of rows in status
is available / available :
+----------+-------------+---------------------+
| element | status | hour |
+----------+-------------+---------------------+
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+----------+-------------+---------------------+
is this means anomaly.
I need to intercept these cases that is, all the rows for each element status
when the timeline is available / available.
How to do resolve this ?
Please can you help me ?
#Edit 01
+-----------+-------------+---------------------+---------+
| element | status | hour | ID |
+-----------+-------------+---------------------+---------+
| 18 | Available | 2020-01-19 14:23:49 | 6 |
| 18 | Unavailable | 2019-09-13 18:19:47 | 5 |
| 18 | Available | 2019-09-13 18:18:49 | 4 |
| 18 | Unavailable | 2019-09-09 08:22:45 | 3 |
| 19 | Available | 2019-09-07 19:13:56 | 2 |
| 19 | Available | 2019-09-03 18:13:49 | 1 |
+-----------+-------------+---------------------+---------+
Based on your originally published sample data you can use a sub query to take a look at the next and previous status then test
select s.element,s.hour,s.`status`
from
(
select t.*,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour < t.hour
order by t1.element,t1.hour desc limit 1) prev,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour > t.hour
order by t1.element,t1.hour limit 1) nxt
from t
) s
where s.status = substring_index(s.nxt,',',1) or
s.status = substring_index(s.prev,',',1)
;