Hi Folks,
Please help me here. I want to get Start datetime and End datetime based on the status column from source table.
Source table Script:
Create table Sourcetabel(Time_Stamp datetime,shift_id Varchar(50),Machine_Code Varchar(50),Line_Code Varchar(50),CompanyCode Varchar(50),PlantCode Varchar(50),Machine_status int)
Insert into Sourcetabel values('2020-10-05 15:00:00','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:00:08','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:00:33','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:00:52','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:01:09','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:01:25','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:01:43','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:02:06','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:02:34','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:02:54','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:03:12','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:03:31','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:03:49','S2','M1','L1','DXC','Unit_1',0)
Insert into Sourcetabel values('2020-10-05 15:04:05','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:04:05','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:06:29','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:07:12','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:07:31','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:07:50','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:08:06','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:08:30','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:08:44','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:08:59','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:09:17','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:09:37','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:09:55','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:10:16','S2','M1','L1','DXC','Unit_1',3)
Insert into Sourcetabel values('2020-10-05 15:10:36','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:10:50','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:11:06','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:11:22','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:11:42','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:11:56','S2','M1','L1','DXC','Unit_1',1)
Insert into Sourcetabel values('2020-10-05 15:12:25','S2','M1','L1','DXC','Unit_1',4)
Insert into Sourcetabel values('2020-10-05 15:12:46','S2','M1','L1','DXC','Unit_1',4)
Insert into Sourcetabel values('2020-10-05 15:13:00','S2','M1','L1','DXC','Unit_1',4)
Insert into Sourcetabel values('2020-10-05 15:13:15','S2','M1','L1','DXC','Unit_1',4)
Insert into Sourcetabel values('2020-10-05 15:13:30','S2','M1','L1','DXC','Unit_1',4)
Expected Output: