Calculating time differences
I am having issues trying to calculate these values:
TABLEA
ID SCHD_FROM SCHD_TO UNSCHD_FROM UNSCHD_TO
123 7/1/2008 7:15:00 AM 7/1/2008 12:30:00 PM
123 7/1/2008 12:30:00 PM 7/1/2008 5:00:00 PM
234 9/1/2008 7:00:00 AM 9/1/2008 1:00:00 PM 9/1/2008 9:00:00 AM 9/1/2008 11:00:00 AM
TABLE B
ID START END
123 7/1/2008 11:30:00 AM 7/1/2008 5:30:00 PM
234 9/1/2008 8:00:00 AM 9/1/2008 1:00:00 PM
Output I want is:
ID IN_MINS OUT_MINS
123 330 30
234 180 120
For row 1 in output:
In_Mins ---------> I want to compare my tableB row with the 2 rows in table A and see how many minutes of start and end FALL between schd_from and schd_end
Out_Mins -------> I want to compare my tableB row with the 2 rows in table A and see how many minutes of start and end DOES NOT FALL between schd_from and schd_end
For row 2 in output:
In_Mins ---------> I want to compare my tableB row with the 2 rows in table A and see how many minutes of start and end FALL between schd_from and schd_end AND see if there is any unschd_from time in it, if there is then I need to subtract it from the total.
Out_Mins -------->Any start and end time that falls between unschd_from and unschd_to should go to out_mins
I am looking for an approach as to how to proceed with it.
Thank you