This is slightly different than the usual gaps-and-islands SQL problem. I have a table that corresponds to the date each person received a batch of medication, and the number of days supply of that medication. This means each row could be viewed as a date range for which the person had medication. A person may receive multiple batches of medication on the same day, and may also receive a new batch of medication when they still have some remaining. I need to find the start and end dates for contiguous date ranges when the person had medication. For example, imagine the input was the table below (which only includes a single person with a single kind of medication):
CREATE TABLE Supply (
start_dt DATE NOT NULL,
days_supply INT NOT NULL;
);
INSERT INTO Supply VALUES
('2022-01-01', 7),
('2022-01-01', 7),
('2022-01-12', 7),
('2022-01-28', 7),
('2022-02-04', 2),
('2022-03-01', 7);
The desired output would look like this:
start_dt | end_dt |
---|---|
2022-01-01 | 2022-01-21 |
2022-01-28 | 2022-02-05 |
2022-03-01 | 2022-03-07 |
I've looked at the usual gaps-and-islands solutions to resolving overlaps between date ranges, but in those, the amount of overlap doesn't matter. In this problem, any overlap effectively extends the end date of the contiguous medication date range.