https://bbs.fanruan.com/thread-133024-1-1.html 参考
++++++
看懂么?用lag()新增一个字段,然后两个字段相减即可
--------------------
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
----------------
with tablea as(
select 1 as t
),
tableb as(
select 2 as s
)
select * from tablea,tableb
---------