Dear Monks,
I have to calculate the time sums of some prolonged procedure “b” in the different departments. The start time of the procedure is the timestamp “start_b”, the end time is “end_b”. The admission timestamp in the dept is “start_a”, the departure timestamp is “end_a”. Sometimes the procedure starts before the admission in the certain dept (i.e. in another dept) and/or ends after the departure from this dept (in the next dept). As the task is to calculate the process periods per department it is declared that the start of the procedure per dept is either the start of the procedure itself (if it is later than the admission timestamp) or the admission timestamp (if it is later than the start of the procedure). The same way the end of the procedure per dept is either the end of the procedure itself (if earlier as the departure timestamp) or the departure timestamp (if earlier as the end of the procedure).
In other words I have to measure the overlapping periods of time. The measure unit of the time is hour.
My today’s solution uses three loops with several breakouts through “next” and “last”. In the real life task with > 10K rows and >100K calculated hours it took proud 15 min (compared with 50 min before using the loop breaks). However I am still not sure that I do not use microscop as a hammer... I would be very glad if you could give me a hint how this could be optimized.
Besides I am thinking about saving the time spans per department in a variable in case to re-use it in the further queries. What kind of a data structure would you suggest here?
Thanks in advance.
VEUpdate: Typos corrected.
The code prints: