I've a SQL table of start times and finish times for several different cycles running through a system. The table below is a simplified sample of the data I'm dealing with.
Batch# Event# Event Timestamp 1 1 Start 2015-07-01 12:31:31.000 1 2 Start 2015-07-01 12:31:31.000 1 3 Start 2015-07-01 12:31:31.000 1 4 End 2015-07-01 12:33:32.000 1 5 End 2015-07-01 12:33:32.000 1 6 End 2015-07-01 12:33:32.000 2 1 Start 2015-07-01 12:35:32.000 2 2 End 2015-07-01 12:36:32.000 I want to produce a table which looks something like this.
Batch# Event# Event Timestamp Event# Event Timestamp 1 1 Start 2015-07-01 12:31:31.000 4 End 2015-07-01 12:33:32.000 1 2 Start 2015-07-01 12:31:31.000 5 End 2015-07-01 12:33:32.000 1 3 Start 2015-07-01 12:31:31.000 6 End 2015-07-01 12:33:32.000 2 1 Start 2015-07-01 12:35:32.000 2 End 2015-07-01 12:36:32.000 I've tried using a left outer join but I'm getting extra rows.
SELECT * FROM Table1 as T1 LEFT OUTER JOIN Table1 as T2 ON T1.Batch_no = T2.Batch_no WHERE T1.Event = 'Start' AND T2.Event = 'End' Batch# Event# Event Timestamp Event# Event Timestamp 1 1 Start 2015-07-01 12:31:31.000 1 4 End 2015-07-01 12:33:32.000 1 1 Start 2015-07-01 12:31:31.000 1 5 End 2015-07-01 12:33:32.000 1 1 Start 2015-07-01 12:31:31.000 1 6 End 2015-07-01 12:33:32.000 1 2 Start 2015-07-01 12:31:31.000 1 4 End 2015-07-01 12:33:32.000 1 2 Start 2015-07-01 12:31:31.000 1 5 End 2015-07-01 12:33:32.000 1 2 Start 2015-07-01 12:31:31.000 1 6 End 2015-07-01 12:33:32.000 1 3 Start 2015-07-01 12:31:31.000 1 4 End 2015-07-01 12:33:32.000 1 3 Start 2015-07-01 12:31:31.000 1 5 End 2015-07-01 12:33:32.000 1 3 Start 2015-07-01 12:31:31.000 1 6 End 2015-07-01 12:33:32.000 2 1 Start 2015-07-01 12:35:32.000 2 2 End 2015-07-01 12:36:32.000