Question:
I have 2 tables, TABLE1:
and a TABLE2:
The issue is on the first table. Due to the overlap of start and end times, I need to be able to distinguish between whether there is an overlap. If there is an overlap, then I need to identify the category as “MULTIPLE”. If there isn’t an overlap, then I only flag the category as. the job_type. When joining to second table, the second table needs to have the job_type populated such that it will either show the one job_type if the time_sample from TABLE2 only falls between the start and end times of only 1 record from TABLE1. if the time_sample falls within more than 1 start/end times for that one job, then it should populate MULTIPLE. I’m having a hard time understanding how to be able to perform this kind of semi-aggregation/join logic to create the following:
The join should be on ‘job’ and where TABLE2.time_sample is between TABLE1.start_time and TABLE1.end_time
Answer:
You can do a simple join between the tables. For the job type check if there are multiple matches:
See
db-fiddle
If you have better answer, please add a comment about this, thank you!