EDIT:met behulp van aanvullende informatie is dit nu beperkt tot het tonen van alleen die boekingen die botsen met een VROEGER boeking.
Ik denk dat dit voldoende zal zijn:
SELECT DISTINCT
b2.booking_id, -- The later booking
b2.booking_start,
b2.booking_end,
b2.creation_date,
rb1.resource_id, -- The resource over which the two bookings clash
b1.booking_id, -- The earlier booking
b1.booking_start,
b1.booking_end
FROM resource_booked rb1
INNER JOIN booking b1 ON b1.booking_id = rb1.booking_id
INNER JOIN booking b2 ON b1.booking_id <> b2.booking_id
AND b2.booking_start BETWEEN b1.booking_start AND b1.booking_end
INNER JOIN resource_booked rb2 ON rb2.resource_id = rb1.resource_id
AND rb2.booking_id = b2.booking_id
Zo werd het getest:
use tempdb
drop table resource_booked
drop table Booking
create table Booking
(
booking_id int,
booking_start datetime,
booking_end datetime,
creation_date datetime
)
create table resource_booked
(
booking_id int,
resource_id int
)
insert Booking values (1, '1 january 2000', '1 march 2000', '1 january 2000')
insert Booking values (2, '1 february 2000', '1 may 2000', '2 january 2000')
insert Booking values (3, '1 april 2000', '1 june 2000', '3 january 2000')
insert Booking values (4, '1 july 2000', '1 august 2000', '4 january 2000')
insert resource_booked values (1, 1)
insert resource_booked values (2, 1)
insert resource_booked values (3, 1)
insert resource_booked values (4, 1)
insert resource_booked values (1, 2)
insert resource_booked values (3, 2)