让我们假设有一个下表:
CREATE TABLE time_interval (
id SERIAL PRIMARY KEY,
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP
);
样本数据:
INSERT INTO time_interval (start_time, end_time) VALUES
('2024-02-10 01:30:00', null), -- pending
('2024-02-10 03:00:00', null), -- pending
('2024-02-10 07:00:00', '2024-02-10 10:30:00'),
('2024-02-10 09:00:00', '2024-02-10 12:00:00'),
('2024-02-10 11:30:00', '2024-02-10 15:00:00'),
('2024-02-10 13:30:00', '2024-02-10 17:30:00'),
('2024-02-10 16:00:00', '2024-02-10 20:00:00'),
('2024-02-10 18:30:00', '2024-02-10 22:00:00'),
('2024-02-10 21:00:00', '2024-02-10 23:30:00');
-你在说什么?
Give me all intervals between 100 including overlapping ones
预期结果:
('2024-02-10 01:30:00', null)
('2024-02-10 03:00:00', null)
('2024-02-10 07:00:00', '2024-02-10 10:30:00')
('2024-02-10 09:00:00', '2024-02-10 12:00:00')
('2024-02-10 11:30:00', '2024-02-10 15:00:00')
('2024-02-10 13:30:00', '2024-02-10 17:30:00')
('2024-02-10 16:00:00', '2024-02-10 20:00:00')
I was trying to use union to split the logic between those that pending and those that not but I am not sure about it.
Here is the simple version:
SELECT *
FROM time_interval
WHERE start_time < '2024-02-10 17:00:00'
AND (end_time is null or end_time > '2024-02-10 10:00:00');
结果很好,但问题是……有那么简单吗?我是不是遗漏了什么?有没有人看到我应该包括的其他角落 case ?