SQL server 时间处理自连接

时间:2023-03-09 02:23:44
SQL server 时间处理自连接
 --延时的订单
select
t1.OrderId order1, t1.VerifyStatusId id1,t1.VerifyDate '时间1',
t2.OrderId order2, t2.VerifyStatusId id2,t2.VerifyDate '时间2',
t1.VerifyStatus '状态1',
t2.VerifyStatus '状态2',
datediff(n, t2.VerifyDate,t1.VerifyDate) '延时'
from T_VerifySituation t1, T_VerifySituation t2
where 1 =1 
and (t1.VerifyStatusId>t2.VerifyStatusId)
and (t1.OrderId=t2.OrderId)
and (datediff(d, t2.VerifyDate,t1.VerifyDate)>1)--延时时长大于一天
and 
(
(t2.VerifyStatus=3024 and t1.VerifyStatus=3013)or--初审用时
(t2.VerifyStatus=3013 and t1.VerifyStatus=3018)or--终审用时
(t2.VerifyStatus=3018 and t1.VerifyStatus=1008)or--综评用时
(t2.VerifyStatus=1019 and t1.VerifyStatus=1010)  --复议用时
)