你好我需要计算这个时间戳事务列的时差,格式如下,我正在试图使用DATEDIFF函数无济于事。谢谢你的帮助。
Txtimestamp
2016-01-05 12:16:51.000
2016-01-05 12:16:51.000
2016-01-18 12:24:16.000
2016-01-18 12:24:16.000
2016-01-20 08:15:32.000
2016-01-20 08:15:32.000
2016-01-20 12:24:29.000
2016-01-20 12:24:29.000
2016-01-29 12:18:11.000
2016-01-29 12:18:11.000
2016-02-01 12:16:43.000
2016-02-01 12:16:43.000
答案 0 :(得分:3)
你想要这样的东西吗?
select datediff(second, lag(txtimestamp) over (order by txtimestamp), txtimestamp) as diff_in_seconds
from t;
答案 1 :(得分:0)
我心中有这样的东西,但它并不像戈登的答案那样清脆。我不确定性能差异是什么。可能很多,因为这需要执行两个子查询...
SELECT tA.txtimestamp, tB.txtimestamp, DATEDIFF(second, tA.txtimestamp, tB.txtimestamp) AS diff
FROM (SELECT row_num = ROW_NUMBER() OVER (ORDER BY txtimestamp), txtimestamp
FROM your_table) AS tA
INNER JOIN
(SELECT row_num = ROW_NUMBER() OVER (ORDER BY txtimestamp), txtimestamp
FROM your_table) AS tB ON tA.row_num = tB.row_num - 1
它很笨重,但肯定是另一种可能性。您需要将连接条件中的“ - ”修改为“+”,具体取决于您希望如何产生差异。