Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

TSQL - To Remove Overlap between 2 sets of records and give priority to 1 set

$
0
0

Hello,

I have two sets of records in a table namely ACTIVITY and DELAY for a particular machine in a particular shiftdate and shift.  During some shiftdates and shifts OVERLAP occurs between Acitivty and Delay .The DELAY records occurring between ACTIVITY records should be given the priority and ACTIVITY records should split accordingly around DELAY records thus removing the OVERLAP between Activity and Delays

Please note that not always the Overlap occurs

Input

Type     Shiftdate          Shift#   MachineID        StartTime          EndTime
Activity1/1/201317801/1/13 7:00 AM1/1/13 8:00 AM
Activity1/1/201317801/1/13 8:00 AM1/1/13 8:40 AM
Activity1/2/201317801/2/13 7:00 AM1/2/13 8:00 AM
Activity1/2/201317801/2/13 8:00 AM1/2/13 8:40 AM
Activity1/1/201317811/1/13 7:00 AM1/1/13 8:00 AM
Activity1/1/201317811/1/13 8:00 AM1/1/13 8:40 AM
Activity1/2/201317811/2/13 7:00 AM1/2/13 8:00 AM
Activity1/2/201317811/2/13 8:00 AM1/2/13 8:40 AM
Delay1/1/201317801/1/13 7:10 AM1/1/13 7:20 AM
Delay1/1/201317801/1/13 7:30 AM1/1/13 7:45 AM
Delay1/1/201317801/1/13 7:51 AM1/1/13 8:03 AM
Delay1/1/201317801/1/13 8:15 AM1/1/13 8:30 AM
Delay1/2/201317801/2/13 6:54 AM1/2/13 7:10 AM
Delay1/2/201317801/2/13 7:30 AM1/2/13 7:45 AM
Delay1/2/201317801/2/13 8:15 AM1/2/13 8:30 AM
Delay1/2/201317801/2/13 8:35 AM1/2/13 8:43 AM
Delay1/1/201317811/1/13 7:10 AM1/1/13 7:20 AM
Delay1/1/201317811/1/13 7:30 AM1/1/13 7:45 AM
Delay1/1/201317811/1/13 7:51 AM1/1/13 8:03 AM
Delay1/1/201317811/1/13 8:15 AM1/1/13 8:30 AM
Delay1/2/201317811/2/13 6:54 AM1/2/13 7:10 AM
Delay1/2/201317811/2/13 7:30 AM1/2/13 7:45 AM
Delay1/2/201317811/2/13 8:15 AM1/2/13 8:30 AM
Delay1/2/201317811/2/13 8:35 AM1/2/13 8:43 AM

Activity                   1/3/2013               1              780         1/3/13 9:00 AM      1/3/13 10:00 AM
DELAY                     1/3/2013              1             780         1/3/13 10:00 AM      1/3/13 11:00 AM

OUTPUT

Type          Shiftdate   Shift#MachineID         StartTime     EndTime
Activity1/1/201317801/1/13 7:00 AM1/1/13 7:10 AM
Delay1/1/201317801/1/13 7:10 AM1/1/13 7:20 AM
Activity1/1/201317801/1/13 7:20 AM1/1/13 7:30 AM
Delay1/1/201317801/1/13 7:30 AM1/1/13 7:45 AM
Activity1/1/201317801/1/13 7:45 AM1/1/13 7:51 AM
Delay1/1/201317801/1/13 7:51 AM1/1/13 8:03 AM
Activity1/1/201317801/1/13 8:03 AM1/1/13 8:15 AM
Delay1/1/201317801/1/13 8:15 AM1/1/13 8:30 AM
Activity1/1/201317801/1/13 8:30 AM1/1/13 8:40 AM
Delay1/2/201317801/2/13 6:54 AM1/2/13 7:10 AM
Activity1/2/201317801/2/13 7:10 AM1/2/13 7:30 AM
Delay1/2/201317801/2/13 7:30 AM1/2/13 7:45 AM
Activity1/2/201317801/2/13 7:45 AM1/2/13 8:00 AM
Activity1/2/201317801/2/13 8:00 AM1/2/13 8:15 AM
Delay1/2/201317801/2/13 8:15 AM1/2/13 8:30 AM
Activity1/2/201317801/2/13 8:30 AM1/2/13 8:35 AM
Delay1/2/201317801/2/13 8:35 AM1/2/13 8:43 AM
Activity1/1/201317811/1/13 7:00 AM1/1/13 7:10 AM
Delay1/1/201317811/1/13 7:10 AM1/1/13 7:20 AM
Activity1/1/201317811/1/13 7:20 AM1/1/13 7:30 AM
Delay1/1/201317811/1/13 7:30 AM1/1/13 7:45 AM
Activity1/1/201317811/1/13 7:45 AM1/1/13 7:51 AM
Delay1/1/201317811/1/13 7:51 AM1/1/13 8:03 AM
Activity1/1/201317811/1/13 8:03 AM1/1/13 8:15 AM
Delay1/1/201317811/1/13 8:15 AM1/1/13 8:30 AM
Activity1/1/201317811/1/13 8:30 AM1/1/13 8:40 AM
Delay1/2/201317811/2/13 6:54 AM1/2/13 7:10 AM
Activity1/2/201317811/2/13 7:10 AM1/2/13 7:30 AM
Delay1/2/201317811/2/13 7:30 AM1/2/13 7:45 AM
Activity1/2/201317811/2/13 7:45 AM1/2/13 8:00 AM
Activity1/2/201317811/2/13 8:00 AM1/2/13 8:15 AM
Delay1/2/201317811/2/13 8:15 AM1/2/13 8:30 AM
Activity1/2/201317811/2/13 8:30 AM1/2/13 8:35 AM
Delay1/2/201317811/2/13 8:35 AM1/2/13 8:43 AM

Activity                   1/3/2013               1              780         1/3/13 9:00 AM      1/3/13 10:00 AM
DELAY                     1/3/2013              1             780         1/3/13 10:00 AM      1/3/13 11:00 AM

Would appreciate the help 




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>