我不确定这是否有帮助,但现在没有更多时间了。我希望它有助于一些如何:
DECLARE @Table TABLE
(
ActualStartTime datetime
, AimMin int
, AimCurrent int
, AimMax int
)
;
INSERT INTO @Table
VALUES
('2020-12-31 10:15', 140, 155, 160)
, ('2021-01-01 18:30', 140, 155, 160)
, ('2021-01-02 01:14', 140, 145, 150)
, ('2021-01-02 11:18', 135, 140, 150)
, ('2021-01-04 10:47', 135, 140, 150)
, ('2021-01-05 09:54', 135, 140, 150)
, ('2021-01-05 16:36', 135, 140, 150)
, ('2021-01-05 19:33', 140, 155, 160)
, ('2021-01-05 23:49', 140, 155, 160)
, ('2021-01-06 20:06', 140, 155, 160)
, ('2021-01-07 10:32', 140, 155, 160)
, ('2021-01-07 16:53', 140, 155, 160)
, ('2021-01-08 09:54', 140, 160, 160)
;
;WITH Table1 AS
(
SELECT TOP 100 PERCENT
RowNo = RANK() OVER(PARTITION BY AimMin, AimCurrent, AimMax ORDER BY ActualStartTime)
, *
FROM @Table
ORDER BY ActualStartTime
)
, Table2 AS
(
SELECT TOP 100 PERCENT
RowNo = RANK() OVER(PARTITION BY AimMin, AimCurrent, AimMax ORDER BY ActualStartTime)
, *
FROM @Table
ORDER BY ActualStartTime
)
SELECT DISTINCT
T1.*
FROM Table1 T1
JOIN Table2 T2 ON T2.RowNo = T1.RowNo+1
WHERE T2.RowNo = 3
AND DATEDIFF(DAY,T1.ActualStartTime, T2.ActualStartTime) BETWEEN 0 AND 2
ORDER BY T1.ActualStartTime