PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Rimozione di eventi parzialmente/totalmente sovrapposti con timestamp di inizio e fine su più valori

La condizione di unione:

Se non riesci a trovare nessun'altra riga significa che sei solo perché non si sovrappongono o perché sei l'ultima data.

DEMO SQL

SELECT a.*
FROM "events" a
LEFT JOIN "events" b
  ON a.room = b.room 
 AND a.id <> b.id
 AND a."start" <= b."end"
 AND a."end"   >= b."start"
 AND a.created_at < b.created_at
WHERE b.id IS NULL;

USCITA

| id |                start |                  end |                  created_at |  room |
|----|----------------------|----------------------|-----------------------------|-------|
|  2 | 2019-01-23T18:30:00Z | 2019-01-23T19:00:00Z | 2019-01-23T01:04:05.861876Z | Room1 |
|  7 | 2019-01-23T20:15:00Z | 2019-01-23T20:45:00Z |  2019-01-20T20:20:20.20202Z | Room1 |
|  9 | 2019-01-23T18:30:00Z | 2019-01-23T19:00:00Z | 2019-01-23T01:04:05.861877Z | Room2 |
| 14 | 2019-01-23T20:15:00Z | 2019-01-23T20:45:00Z |  2019-01-20T20:20:20.20202Z | Room2 |
| 16 | 2019-01-23T20:15:00Z | 2019-01-23T20:45:00Z | 2019-01-20T20:20:20.202021Z | Room3 |