I am looking for the most efficient way to accomplish the following ...
I have two tables. One that stores time ranges indicating production downtime, and one that stores incremental units produced as recorded at various timestamps.
Downtime:
| Start Time | End Time | Duration (Mins) |
| 5/26/2019 8:17:44 | 5/26/2019 8:18:44 | 1 |
| 5/26/2019 9:13:04 | 5/26/2019 9:36:24 | 23.3 |
| 5/26/2019 9:46:44 | 5/26/2019 9:50:44 | 4 |
| 5/26/2019 9:52:04 | 5/26/2019 9:52:59 | 0.9 |
| 5/26/2019 11:13:35 | 5/26/2019 11:16:25 | 2.8 |
Production:
| time | value |
| 5/26/2019 8:17:35 | 1104 |
| 5/26/2019 8:17:43 | 1196 |
| 5/26/2019 8:17:51 | 1288 |
| 5/26/2019 8:17:57 | 1380 |
| 5/26/2019 8:18:03 | 1472 |
| 5/26/2019 8:18:11 | 1564 |
| 5/26/2019 8:18:17 | 1656 |
| 5/26/2019 8:18:23 | 1748 |
| 5/26/2019 8:18:31 | 1840 |
| 5/26/2019 8:18:37 | 1932 |
| 5/26/2019 8:18:43 | 2024 |
| 5/26/2019 8:18:51 | 2116 |
For each record in the Downtime table, I want to capture the min and max values of the records in the Production table where the Production timestamp falls within the Start Time and End Time. This is so I can determine the number of units produced during the downtime period (Max Units - Min Units)
Output:
| Start Time | End Time | Duration (Mins) | Min Units | Max Units | Units Produced |
| 5/26/2019 8:17:44 | 5/26/2019 8:18:44 | 1 | 1288 | 2024 | 736 |
For reference, the Downtime table would typically have about 100 entries per day and the Production table would have 40-50,000 records per day. I can see this workflow being run for date ranges up to a full year, so there could be a massive amount of records to process.
I am also open to other suggestions on how to achieve the same results.
Thanks in advance