从连续行时间戳访问2013查询,DateDiff

时间:2019-05-23 18:47:23

标签: rows access datediff sequential

我遇到了一个问题,该问题使用SQL在Access 2013的单个表上成功完成(运行)查询,以在时间戳的连续/顺序行上完成Datediff,该行可跟踪通过我们的票务系统的票证中的状态变化。 / p>

标题为dbo_Master3_FieldHistory的表具有一个字段,该字段可在每次票证状态更改时跟踪时间戳。不幸的是,每次更改仅包含1个时间戳,这意味着它在本质上没有用于再次更改状态的辅助时间戳,我需要运行DateDiff来根据状态计算票证的年龄。

我在StackOverflow上找到了一个可行的解决方案,链接如下。当我尝试实施此解决方案时,需要进行一些小的调整,包括对旧数据和特定字段进行过滤的调整,它只会冻结我的Access程序,并且永远不会超时(必须强制关闭Access)

Date Difference between consecutive rows

'这是基本代码,是从链接的StackOverflow解决方案翻译过来的,以适合此表字段(我相信)

SELECT T.mrID, T.mrSEQUENCE, T.mrUSERID, T.mrFIELDNAME, T.mrNEWFIELDVALUE, T.mrOLDFIELDVALUE, T.mrTIMESTAMP, T.mrNextTIMESTAMP, DateDiff("s",T.mrTIMESTAMP, T.mrNextTIMESTAMP) AS STATUSTIME
FROM ( 

SELECT T1.mrID, T1.mrSEQUENCE, T1.mrUSERID, T1.mrFIELDNAME, T1.mrNEWFIELDVALUE, T1.mrOLDFIELDVALUE, T1.mrTIMESTAMP, 

(SELECT MIN(mrTIMESTAMP)
FROM dbo_MASTER3_FIELDHISTORY AS T2
WHERE T2.mrID = T1.mrID
AND T2.mrTIMESTAMP > T1.mrTIMESTAMP
) As mrNextTIMESTAMP

FROM dbo_MASTER3_FIELDHISTORY AS T1

) AS T

'这是我想用来过滤出两个特定字段的代码,将数据限制为比2018年1月1日更新的票证(mrID),并且仅限那些mrFIELDNAME为mrSTATUS的票证

SELECT T.mrID, T.mrSEQUENCE, T.mrUSERID, T.mrFIELDNAME, T.mrNEWFIELDVALUE, T.mrOLDFIELDVALUE, T.mrTIMESTAMP, T.mrNextTIMESTAMP, DateDiff("s",T.mrTIMESTAMP, T.mrNextTIMESTAMP) AS STATUSTIME
FROM ( 

SELECT T1.mrID, T1.mrSEQUENCE, T1.mrUSERID, T1.mrFIELDNAME, T1.mrNEWFIELDVALUE, T1.mrOLDFIELDVALUE, T1.mrTIMESTAMP, 

(SELECT MIN(mrTIMESTAMP)
FROM dbo_MASTER3_FIELDHISTORY AS T2
WHERE mrFIELDNAME = "mrSTATUS"
AND T2.mrID = T1.mrID
AND T2.mrTIMESTAMP > T1.mrTIMESTAMP
) As T1.mrNextTIMESTAMP

FROM dbo_MASTER3_FIELDHISTORY AS T1
WHERE mrFIELDNAME = "mrSTATUS"
AND mrTIMESTAMP >= #1/1/2018#

) AS T;

当我尝试运行这些查询时,访问会冻结。我尝试了几种方法,但无法正常工作

1 个答案:

答案 0 :(得分:0)

我能够弄清楚,谢谢那些花时间阅读这一有趣挑战的人。我没有使用提供的链接中的第二个代码集,而是利用了第一个,并且效果很好。在代码中添加了一些内容以说明其他过滤器/条件后,我得到了所需的结果。


SELECT T1.mrID, T1.mrSEQUENCE, T1.mrUSERID, T1.mrFIELDNAME, T1.mrNEWFIELDVALUE, T1.mrOLDFIELDVALUE, T1.mrTIMESTAMP, MIN(T2.mrTIMESTAMP) AS mrNextTIMESTAMP, DATEDIFF("s", T1.mrTIMESTAMP, MIN(T2.mrTIMESTAMP)) AS TimeInStatus
FROM ((dbo_MASTER3_FIELDHISTORY AS T1 LEFT JOIN dbo_MASTER3_FIELDHISTORY AS T2 ON (T2.mrTIMESTAMP > T1.mrTIMESTAMP) AND (T1.mrID = T2.mrID)) INNER JOIN dbo_MASTER3 AS T4 ON (T4.mrID = T1.mrID))
WHERE T4.mrSUBMITDATE >= #1/1/2018#
AND t1.mrFIELDNAME = "mrSTATUS"
AND NOT T4.mrSTATUS="_Deleted_"
AND NOT T4.mrSTATUS="_SOLVED_"
AND NOT T4.mrSTATUS="_PENDING_SOLUTION_"
GROUP BY T1.mrID, T1.mrSEQUENCE, T1.mrUSERID, T1.mrFIELDNAME, T1.mrNEWFIELDVALUE, T1.mrOLDFIELDVALUE, T1.mrTIMESTAMP
ORDER BY T1.mrID, T1.mrTIMESTAMP;

此致, 克里斯托弗