Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exclude rows from MySQL table where timestamp is less than in other row

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I filter these rows out of the table using MySQL or maybe Python’s pandas?

>Solution :

The easiest way would be to GROUP BY the begintime and getting the maximum endtime:

SELECT begintime, MAX(endtime) AS endtime
FROM your_table
GROUP BY begintime
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading