程序代码:
with test as (
select '2023-02-04 19:08:52' [rq_]
union all
select '2023-02-04 19:05:54'
union all
select '2023-02-04 19:03:23'
union all
select '2023-02-04 19:01:43'
union all
select '2023-02-04 19:00:43'
union all
select '2023-02-04 18:57:08'
union all
select '2023-02-04 18:53:20'
)
SELECT T.*,DATEDIFF(SS,rq_,NEW_) [时差] FROM (
select A.*,(SELECT TOP 1 B.RQ_ FROM TEST B WHERE A.rq_>B.rq_ ORDER BY B.rq_ DESC) [NEW_] FROM test A
) T
select '2023-02-04 19:08:52' [rq_]
union all
select '2023-02-04 19:05:54'
union all
select '2023-02-04 19:03:23'
union all
select '2023-02-04 19:01:43'
union all
select '2023-02-04 19:00:43'
union all
select '2023-02-04 18:57:08'
union all
select '2023-02-04 18:53:20'
)
SELECT T.*,DATEDIFF(SS,rq_,NEW_) [时差] FROM (
select A.*,(SELECT TOP 1 B.RQ_ FROM TEST B WHERE A.rq_>B.rq_ ORDER BY B.rq_ DESC) [NEW_] FROM test A
) T