I would like to do the following transformation in SQL Server database.
Aggregate over Tag_of_thing & Status_of_thing such that...
Note in this case end shift is the row above, see example tables
If Start_shift_date_time = End_shift_date_time then aggregate rows such that start = min(Start_shift_date_time ) and finish = max(End_shift_date_time )
And if Start_shift_date_time <= End_shift_date_time + 10mins then aggregate rows such that start = min(Start_shift_date_time ) and finish = max(End_shift_date_time )
Raw Source Data Table (lets call this table input)
Tag_of_thing Status_of_thing Start_shift_date_time End_shift_date_time Other_column_I_dont_care_about
A123 T123 04/08/2020 15:07:02 04/08/2020 18:00:00 56110
A123 T123 04/08/2020 18:00:00 05/08/2020 01:27:41 32473
A123 T123 05/08/2020 06:15:41 05/08/2020 06:00:00 26808
A123 T124 05/08/2020 06:00:00 05/08/2020 18:00:00 23969
A123 T124 05/08/2020 18:00:00 06/08/2020 06:00:00 29613
A123 T124 06/08/2020 06:00:00 06/08/2020 10:48:00 7276
B124 G1 03/08/2020 12:43:02 03/08/2020 18:00:00 29806
B124 G1 03/08/2020 18:00:00 03/08/2020 23:03:41 46101
B124 G1 03/08/2020 23:07:41 04/08/2020 03:55:41 15510
C124 R1 03/08/2020 12:43:02 03/08/2020 18:00:00 47527
C124 R1 03/08/2020 18:00:00 03/08/2020 23:03:41 16708
C124 R1 03/08/2020 23:18:41 04/08/2020 04:06:41 3247
Transformed Output Data Table
Tag_of_thing Status_of_thing Start_shift_date_time End_shift_date_time
A123 T123 04/08/2020 15:07:02 05/08/2020 01:27:41
A123 T123 05/08/2020 06:15:41 05/08/2020 06:00:00
A123 T124 05/08/2020 06:00:00 06/08/2020 10:48:00
B124 G1 03/08/2020 12:43:02 04/08/2020 03:55:41
C124 R1 03/08/2020 12:43:02 03/08/2020 23:03:41
C124 R1 03/08/2020 23:18:41 04/08/2020 04:06:41
Please let me know if you need more examples or a different explanation of the transformation process
Resources
Similar Gaps & Island Problem: https://bertwagner.com/2019/03/12/gaps-and-islands/