从多个不同的行中查找 MSSQL 中 2 个日期之间的差异
Finding the Difference between 2 Dates in MSSQL from multiple different Rows
当使用 MSSQL 时,有什么方法可以使用 SQL 语句对 2 个不同行的日期进行 DATEDIFF。我们正在尝试衡量响应时间。
我们有一个消息传递系统,用于存储 UniqueKey、Note、DateNoteSent 和发送笔记的用户。
数据样本:
UniqueKey Note User DateOfNote
L4H2390039D2 This is a test. 23 2018-01-30 16:15:14.2965479
L4H2390039D2 Test received. 15 2018-01-30 18:10:00.3482044
DH38D2DJ8382 Call me ASAP. 17 2018-01-30 16:22:34.1971033
DH38D2DJ8382 Will do. 35 2018-01-30 16:25:34.1749088
DH38D2DJ8382 Sorry I missed you. 17 2018-01-30 16:28:34.1991463
是否可以找出具有相同UniqueKey的消息中的date/times之间的区别?
有些消息在链中有超过 2 条消息,并且会。
我想要最后一列显示上一条名为 ResponseTime 的消息的持续时间,显示时间即:12:34:33 或 1 小时 37 分钟。
感谢您的帮助。
如果您在 2012 年之前使用 SQL,那么替代 LEAD 函数:
create Table #Table1 (
UniqueKey varchar(20),
Note varchar(100),
[User] varchar(10),
DateOfNote datetime
);
-- add some records
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('L4H2390039D2','This is a test.','23',CAST(N'2018-01-30 16:15:14.296' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('L4H2390039D2','Test received.','15',CAST(N'2018-01-30 18:10:00.348' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Call me ASAP','17',CAST(N'2018-01-30 16:22:34.197' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Will do.','35',CAST(N'2018-01-30 16:25:34.174' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Sorry I missed you.','17',CAST(N'2018-01-30 16:28:34.199' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Sorry I missed you.','17',CAST(N'2018-02-02 08:01:34.199' AS DateTime))
select t.Number,t.UniqueKey,t.DateofNote,tnext.DateofNote,datediff(minute, t.DateofNote, tnext.DateofNote) as DiffMinutes,CONVERT(varchar(12),
DATEADD(minute, DATEDIFF(minute, t.DateofNote, tnext.DateofNote), 0), 114) as hourMinute,
cast(datediff(minute, t.DateofNote, tnext.DateofNote) / 1440 as varchar) + ' days ' + cast((datediff(minute, t.DateofNote, tnext.DateofNote) % 1440) / 60 as varchar) + ' hours ' + cast(datediff(minute, t.DateofNote, tnext.DateofNote) % 60 as varchar) + ' minutes' as DaysHoursMinutes
from (
select *
,ROW_NUMBER() over ( ORDER BY UniqueKey,DateOfNote) AS Number
from #Table1
)t join
(select *
,ROW_NUMBER() over ( ORDER BY UniqueKey,DateOfNote) AS Number
from #Table1
) tnext
on t.Number = tnext.Number - 1 and t.UniqueKey = tnext.UniqueKey
order by t.UniqueKey,t.DateofNote
这是一个使用 LAG
函数的解决方案。结果的格式为 hhh:mm:ss
select
UniqueKey, Note, [User], DateOfNote
, concat(right(concat('000',diff/3600), 3), ':', right(concat('00',diff%3600/60), 2), ':', right(concat('00',diff%60), 2))
from (
select
*, diff = datediff(ss, lag(DateOfNote) over (partition by UniqueKey order by DateOfNote), DateOfNote)
from
#Table1
) t
当使用 MSSQL 时,有什么方法可以使用 SQL 语句对 2 个不同行的日期进行 DATEDIFF。我们正在尝试衡量响应时间。
我们有一个消息传递系统,用于存储 UniqueKey、Note、DateNoteSent 和发送笔记的用户。
数据样本:
UniqueKey Note User DateOfNote
L4H2390039D2 This is a test. 23 2018-01-30 16:15:14.2965479
L4H2390039D2 Test received. 15 2018-01-30 18:10:00.3482044
DH38D2DJ8382 Call me ASAP. 17 2018-01-30 16:22:34.1971033
DH38D2DJ8382 Will do. 35 2018-01-30 16:25:34.1749088
DH38D2DJ8382 Sorry I missed you. 17 2018-01-30 16:28:34.1991463
是否可以找出具有相同UniqueKey的消息中的date/times之间的区别?
有些消息在链中有超过 2 条消息,并且会。
我想要最后一列显示上一条名为 ResponseTime 的消息的持续时间,显示时间即:12:34:33 或 1 小时 37 分钟。
感谢您的帮助。
如果您在 2012 年之前使用 SQL,那么替代 LEAD 函数:
create Table #Table1 (
UniqueKey varchar(20),
Note varchar(100),
[User] varchar(10),
DateOfNote datetime
);
-- add some records
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('L4H2390039D2','This is a test.','23',CAST(N'2018-01-30 16:15:14.296' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('L4H2390039D2','Test received.','15',CAST(N'2018-01-30 18:10:00.348' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Call me ASAP','17',CAST(N'2018-01-30 16:22:34.197' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Will do.','35',CAST(N'2018-01-30 16:25:34.174' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Sorry I missed you.','17',CAST(N'2018-01-30 16:28:34.199' AS DateTime))
Insert into #Table1 (UniqueKey,Note,[User],DateOfNote) values ('DH38D2DJ8382','Sorry I missed you.','17',CAST(N'2018-02-02 08:01:34.199' AS DateTime))
select t.Number,t.UniqueKey,t.DateofNote,tnext.DateofNote,datediff(minute, t.DateofNote, tnext.DateofNote) as DiffMinutes,CONVERT(varchar(12),
DATEADD(minute, DATEDIFF(minute, t.DateofNote, tnext.DateofNote), 0), 114) as hourMinute,
cast(datediff(minute, t.DateofNote, tnext.DateofNote) / 1440 as varchar) + ' days ' + cast((datediff(minute, t.DateofNote, tnext.DateofNote) % 1440) / 60 as varchar) + ' hours ' + cast(datediff(minute, t.DateofNote, tnext.DateofNote) % 60 as varchar) + ' minutes' as DaysHoursMinutes
from (
select *
,ROW_NUMBER() over ( ORDER BY UniqueKey,DateOfNote) AS Number
from #Table1
)t join
(select *
,ROW_NUMBER() over ( ORDER BY UniqueKey,DateOfNote) AS Number
from #Table1
) tnext
on t.Number = tnext.Number - 1 and t.UniqueKey = tnext.UniqueKey
order by t.UniqueKey,t.DateofNote
这是一个使用 LAG
函数的解决方案。结果的格式为 hhh:mm:ss
select
UniqueKey, Note, [User], DateOfNote
, concat(right(concat('000',diff/3600), 3), ':', right(concat('00',diff%3600/60), 2), ':', right(concat('00',diff%60), 2))
from (
select
*, diff = datediff(ss, lag(DateOfNote) over (partition by UniqueKey order by DateOfNote), DateOfNote)
from
#Table1
) t