I have a table which looks like below:
CREATE TABLE time_records ( id uuid NOT NULL, employee_id uuid NOT NULL, starttime timestampt NOT NULL, endtime timestampt NOT NULL ) There will be overlap in times between the records for the same employee_id:
| id | employee_id | starttime | endtime |
|---|---|---|---|
| 1 | 1 | '2023-09-01 07:00:00' | '2023-09-01 09:15:00' |
| 2 | 1 | '2023-09-01 07:00:00' | '2023-09-01 15:00:00' |
| 3 | 1 | '2023-09-01 07:00:00' | '2023-09-01 15:00:00' |
| 4 | 1 | '2023-09-01 14:00:00' | '2023-09-01 15:00:00' |
| 5 | 1 | '2023-09-01 23:45:00' | '2023-09-01 23:59:00' |
| 6 | 1 | '2023-09-01 23:45:00' | '2023-09-01 23:59:00' |
What I'm trying to do is get the time ranges within all of these times:
| employee_id | starttime | endtime | ids |
|---|---|---|---|
| 1 | '2023-09-01 07:00:00' | '2023-09-01 15:00:00' | [1,2,3,4] |
| 1 | '2023-09-01 23:45:00' | '2023-09-01 23:29:00' | [5,6] |
I can get this to work if there's only one set of overlapping time within a day using max/min for the start and end times, but I can't seem to make it work when there are multiple sets of overlapping time in a day:
select timea.employee_id, min(timea.starttime) starttime, max(timea.endtime) endtime, array_agg(timea.id) ids from time_records timea inner join time_records timea2 on timea.employee_id = timea2.employee_id and tsrange(timea2.starttime, timea2.endtime, '[]') && tsrange(timea.starttime, timea.endtime, '[]') and timea.id != timea2.id group by timea.employee_id; With results:
| employee_id | starttime | endtime | ids |
|---|---|---|---|
| 1 | '2023-09-01 07:00:00' | '2023-09-01 23:59:00' | [1,2,3,4,5,6] |
timestamportimestamptz. There is no middle ground ("timestampt"). Typically, it should betimestamptz.