我试图在存储的对话中找出两个特定点之间的时间差。这几点在每一次谈话中都会有所不同,这使我很难做到。我需要Agent消息和它之后的第一个EndUser响应之间的时间差。
在下面CaseNr 1234的例子中,我需要MessageNrs 3&4、5&6和7&8之间的时差。在CaseNr 2345中,我需要MessageNrs 3&4、5&6、7&8和10&11之间的时差。在CaseNr 4567中,我需要2&3和4&5之间的时差。
如图所示,order & EndUser可以在每个会话中以及这些类型所处的位置上有所不同。
有没有一种方法可以像我在SQL server中描述的那样计算时差?

发布于 2021-05-06 12:38:57
我觉得这个密码应该对你有帮助。
with t(MessageNr,CaseNr,Type, AgentTime, EndUserTime) as
(
select
t1.MessageNr,
t1.CaseNr,
t1.Type,
t1.EntryTime,
(select top 1 t2.EntryTime
from [Your_Table] as t2
where t1.CaseNr = t2.CaseNr
and t2.[Type] = 'EndUser'
and t1.EntryTime < t2.EntryTime
order by t2.EntryTime) as userTime
from [Your_Table] as t1
where t1.[Type] = 'Agent'
)
select t.*, DATEDIFF(second, AgentTime, EndUserTime)
from t;发布于 2021-05-06 13:46:51
所需的逻辑似乎是Agent行与紧接着的EndUser行之间的时间差。
您可以使用LEAD**,来实现这一点,这将比使用自联接更具有性能。
SELECT *,
DATEDIFF(second, t.EntryTime, t.NextTime) TimeDifference
FROM (
SELECT *,
LEAD(CASE WHEN t.[Type] = 'EndUser' THEN t.EntryTime END) NextTime
FROM myTable t
) t
WHERE t.[Type] = 'Agent'
AND t.NextTime IS NOT NULLhttps://stackoverflow.com/questions/67417653
复制相似问题