I have a MySQL table having "datetime" columns begintime
and endtime
:
+---------------------+---------------------+
| begintime | endtime |
+---------------------+---------------------+
| 2024-05-22 10:13:23 | 2024-05-31 13:37:34 |
| 2024-05-30 17:03:21 | 2024-05-31 16:01:25 |
| 2024-05-30 17:03:21 | 2024-05-31 16:01:25 |
| 2024-05-30 17:03:21 | 2024-05-31 16:01:25 |
| 2024-05-31 15:00:00 | 2024-05-31 15:00:03 |
| 2024-05-31 15:01:32 | 2024-05-31 16:01:26 |
+---------------------+---------------------+
This table contains the rows where begintime
is the same as in some row and endtime
is less than in that row. For example:
| 2024-05-22 10:13:23 | 2024-05-31 12:02:18 |
Here begintime
is the same as in the first row and endtime
is less than in that row.
How can I filter these rows out of the table using MySQL
or maybe Python’s pandas
?
3
Answers
The easiest way would be to
GROUP BY
thebegintime
and getting the maximumendtime
:You can do it by simply using a self-join in MySQL
you can use a self-join in SQL. Here’s how you can do it:
If you want to achieve the same using Python’s pandas: