I've tried to delete unnecessary data from a table, but it hasn't worked for a different date.
I got the query from here
This is my query:
WITH date AS
(SELECT ID, 
MIN(time) AS minDate, 
MAX(time) AS maxDate
FROM tablename
GROUP BY ID)
DELETE FROM tablename
  WHERE 
    NOT EXISTS 
      (SELECT 1 FROM tablename
        WHERE tablename.ID = d.ID 
          AND tablename.time IN (d.minDate, d.maxDate));
This is the table:
| ID | name | time | date | dir | 
|---|---|---|---|---|
| 4 | Ety | 07:51:48 | 2023-07-20 | in | 
| 4 | Ety | 17:04:07 | 2023-07-20 | out | 
| 4 | Ety | 07:50:48 | 2023-07-20 | in | 
| 4 | Ety | 17:08:07 | 2023-07-21 | out | 
| 4 | Ety | 07:08:07 | 2023-07-21 | in | 
| 6 | Herry | 07:26:03 | 2023-07-20 | in | 
| 6 | Herry | 07:16:11 | 2023-07-20 | in | 
| 6 | Herry | 17:26:11 | 2023-07-20 | out | 
| 6 | Herry | 06:26:11 | 2023-07-21 | in | 
| 6 | Herry | 07:12:11 | 2023-07-21 | in | 
| 6 | Herry | 17:26:11 | 2023-07-21 | out | 
| 7 | Martha | 07:50:23 | 2023-07-20 | in | 
| 7 | Martha | 17:04:43 | 2023-07-20 | out | 
| 7 | Martha | 07:50:24 | 2023-07-21 | in | 
| 7 | Martha | 17:04:44 | 2023-07-21 | out | 
| 8 | Martha | 17:14:45 | 2023-07-21 | out | 
Expected table:
| ID | name | time | date | dir | 
|---|---|---|---|---|
| 4 | Ety | 07:51:48 | 2023-07-20 | in | 
| 4 | Ety | 17:04:07 | 2023-07-20 | out | 
| 4 | Ety | 07:08:07 | 2023-07-21 | in | 
| 4 | Ety | 17:08:07 | 2023-07-21 | out | 
| 6 | Herry | 07:16:11 | 2023-07-20 | in | 
| 6 | Herry | 17:26:11 | 2023-07-20 | out | 
| 6 | Herry | 06:26:11 | 2023-07-21 | in | 
| 6 | Herry | 17:26:11 | 2023-07-21 | out | 
| 7 | Martha | 07:50:24 | 2023-07-20 | in | 
| 7 | Martha | 17:04:44 | 2023-07-20 | out | 
| 7 | Martha | 07:50:24 | 2023-07-21 | in | 
| 7 | Martha | 17:14:45 | 2023-07-21 | out | 
 
     
    