Please try the following solution.
SQL
DECLARE @tbl TABLE (Asset_Id INT, Status_Id INT, Update_Date DATETIME);
INSERT @tbl (Asset_Id, Status_Id, Update_Date) VALUES
(1, 1, '2021-06-29 01:00:00'),
(1, 1, '2021-06-29 04:00:00'),
(1, 3, '2021-06-29 05:00:00'),
(1, 3, '2021-06-29 05:00:31'),
(1, 1, '2021-06-29 05:01:00'),
(1, 1, '2021-06-29 05:08:00'),
(1, 2, '2021-06-30 12:12:12'),
(1, 2, '2021-06-30 12:15:12'),
(1, 2, '2021-07-30 04:12:12');
;WITH rs AS
(
SELECT *
, series = status_id + ROW_NUMBER() OVER (ORDER BY Update_Date ASC) -
ROW_NUMBER() OVER (PARTITION BY Asset_ID, Status_Id ORDER BY Update_Date ASC)
FROM @tbl
)
SELECT Asset_Id, Status_Id, MIN(Update_Date) AS Start_date, MAX(rs.Update_Date) AS End_Date
FROM rs
GROUP BY Asset_Id, Status_Id, series
ORDER BY MIN(Update_Date) ASC;
SQL #2
To satisfy the following
the end_date didn't included the time until the next row start time
minus a second
;WITH rs AS
(
SELECT *
, series = status_id + ROW_NUMBER() OVER (ORDER BY Update_Date ASC) -
ROW_NUMBER() OVER (PARTITION BY Asset_Id, Status_Id ORDER BY Update_Date ASC)
, DATEADD(SECOND,-1, LEAD(Update_Date,1) OVER (ORDER BY Update_Date ASC)) AS end_date
FROM @tbl
)
SELECT Asset_Id, Status_Id, MIN(rs.Update_Date) AS Start_date, MAX(COALESCE(rs.end_date,'2099-12-31')) AS End_Date
FROM rs
GROUP BY Asset_Id, Status_Id, series
ORDER BY MIN(rs.Update_Date) ASC;
Output
+----------+-----------+-------------------------+-------------------------+
| Asset_Id | Status_Id | Start_date | End_Date |
+----------+-----------+-------------------------+-------------------------+
| 1 | 1 | 2021-06-29 01:00:00.000 | 2021-06-29 04:00:00.000 |
| 1 | 3 | 2021-06-29 05:00:00.000 | 2021-06-29 05:00:31.000 |
| 1 | 1 | 2021-06-29 05:01:00.000 | 2021-06-29 05:08:00.000 |
| 1 | 2 | 2021-06-30 12:12:12.000 | 2021-07-30 04:12:12.000 |
+----------+-----------+-------------------------+-------------------------+
Output #2
+----------+-----------+-------------------------+-------------------------+
| Asset_Id | Status_Id | Start_date | End_Date |
+----------+-----------+-------------------------+-------------------------+
| 1 | 1 | 2021-06-29 01:00:00.000 | 2021-06-29 04:59:59.000 |
| 1 | 3 | 2021-06-29 05:00:00.000 | 2021-06-29 05:00:59.000 |
| 1 | 1 | 2021-06-29 05:01:00.000 | 2021-06-30 12:12:11.000 |
| 1 | 2 | 2021-06-30 12:12:12.000 | 2099-12-31 00:00:00.000 |
+----------+-----------+-------------------------+-------------------------+