I have searched a bit and not come across anything comparable, so I'm asking this question here, hoping I haven't overlooked anything.
In my situation, we store deliveries on the database, which contain their time interval from start of delivery to the end. This looks roughly like this:
create table delivery_master (
id uuid primary key,
order_id uuid not null,
start_time timestamp with time zone not null,
end_time timestamp with time zone not null,
delivery_id uuid null,
quantity numeric(7,2) not null,
vehicle_id uuid null,
center_id uuid not null,
unique(delivery_id)
);
As vehicles are assigned to certain centers, we first check which vehicles are assigned to what center and then use that list of vehicle_id
s to proceed. This part is a relatively easy lookup.
Now, I get a new order, and I calculated how many deliveries they have and their start and end time. I now want to query the DB, to see which of the vehicles that are assigned, have open spaces to fit these deliveries in.
This has some additional problems: A vehicle can be assigned to multiple delivery centers (i.e. imagine that both are relatively close so they could in theory on their way pickup something from the second center).
My current query looks like this:
select pdm.*
from delivery_master dm,
unnest(
'{2023-05-04T08:00:00Z, 2023-05-04T09:07:00Z}'::timestamptz[],'{2023-05-04T10:00:00Z, 2023-05-04T12:00:00Z}'::timestamptz[]
) periods(stime, etime)
where dm.vehicle_id in ('0a2c7f64-f465-4501-8a98-6ebb255dcf98', '87bc94dc-4da8-4f47-9b47-a79257997ce0')
and not (dm.start_time,dm.end_time) OVERLAPS (periods.stime, periods.etime)
and date(dm.start_time) = '2023-05-04';
Now, in my case I have 2 vehicles that are assigned to the center that should deliver the order. With that list, I now also supply a list of all start_time
and end_time
of the predictions I just made. My goal is to check, for all vehicles, if any have timeslots not overlapping said predictions. In that case, I want it to return all the rows of said vehicles; so that I can do manual assignment on the backend.
My problem now is, that the returned rows only contain the rows that don't overlap. In my case I would want, that if any of the "viable" rows doesn't overlap, to return all rows.
EDIT: After a request, this would be an example dataset for it. The current query would only return a part of this dataset, but the goal would be for it to return all of them.
insert into delivery_master values
('cb96ca55-8d00-43da-a015-fd1f3a8283d2','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 07:33:00+00','2023-05-04 09:06:00+00','d5d6a4ce-d552-444b-b983-b299b5a321da',8.00,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('cb96ca55-8d00-43da-a015-fd1f3a8283d2','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 07:33:00+00','2023-05-04 09:06:00+00','d5d6a4ce-d552-444b-b983-b299b5a321da',8.00,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('8e3f8cd6-7a11-4759-887e-e6970d3381b6','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 06:21:00+00','2023-05-04 07:52:00+00','3cfc2389-1247-47cf-86bb-d1741f673cda',8.00,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('8e3f8cd6-7a11-4759-887e-e6970d3381b6','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 06:21:00+00','2023-05-04 07:52:00+00','3cfc2389-1247-47cf-86bb-d1741f673cda',8.00,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('5273b249-58ee-4636-af7a-dadd3575bd86','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 08:39:00+00','2023-05-04 10:12:00+00','4792cc4d-875e-421c-8f5c-b5d46f17c604',2.50,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('5273b249-58ee-4636-af7a-dadd3575bd86','f00fb9dc-b212-4be3-a936-df1f3420b215','2023-05-04 08:39:00+00','2023-05-04 10:12:00+00','4792cc4d-875e-421c-8f5c-b5d46f17c604',2.50,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('80322214-66c9-46dd-88f5-d949ffa58c89','230c9ea8-60fe-4472-894a-7fe866a0383c','2023-05-04 07:25:00+00','2023-05-04 09:38:00+00','cc76622d-f9ed-4f25-b5cb-bf8ad01c29f6',7.50,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('80322214-66c9-46dd-88f5-d949ffa58c89','230c9ea8-60fe-4472-894a-7fe866a0383c','2023-05-04 07:25:00+00','2023-05-04 09:38:00+00','cc76622d-f9ed-4f25-b5cb-bf8ad01c29f6',7.50,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('db7e0c5a-9b54-4133-939e-5bed5487943e','6b0bac44-ded2-41d8-b581-7c798f52a22f','2023-05-04 10:25:00+00','2023-05-04 11:32:00+00','a1e66278-cc3d-4f9b-bd80-913628d5a7bd',2.00,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('db7e0c5a-9b54-4133-939e-5bed5487943e','6b0bac44-ded2-41d8-b581-7c798f52a22f','2023-05-04 10:25:00+00','2023-05-04 11:32:00+00','a1e66278-cc3d-4f9b-bd80-913628d5a7bd',2.00,'87bc94dc-4da8-4f47-9b47-a79257997ce0','2dfa4013-a312-4247-9f29-3e21631ace6b'),
('defa7fff-77b5-4291-99d3-2ac39abb3b69','48e2a591-7b34-4291-91f0-955a583f6e97','2023-05-04 16:30:33+00','2023-05-04 19:30:33+00',null,7.00,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','9f14bbb3-279b-4e7a-8082-8bb89cfa0b3d'),
('defa7fff-77b5-4291-99d3-2ac39abb3b69','48e2a591-7b34-4291-91f0-955a583f6e97','2023-05-04 16:30:33+00','2023-05-04 19:30:33+00',null,7.00,'0a2c7f64-f465-4501-8a98-6ebb255dcf98','9f14bbb3-279b-4e7a-8082-8bb89cfa0b3d');