Sammenføjningsbetingelsen:
- Prøv at finde en anden begivenhed med samme rum
- Men skal have et andet id end dig
- Skal overlappe datoer Afgør, om to datointervaller overlapper
- Skal endelig have en senere oprettet dato.
Hvis du ikke kan finde en anden række, betyder det, at du er alene, enten fordi du ikke overlapper hinanden, eller fordi du er den seneste dato.
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;
OUTPUT
| 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 |