0

我需要第 1 行和第 2 行之间的时间差。然后对于第 2 行和第 3 行。是否有可以执行此操作的查询?我的同事已经认为这是不可能的。非常感谢您提前提供的帮助。

2012-03-08 15:08:02.260
2012-03-08 15:08:07.180
2012-03-08 15:15:09.220
2012-03-08 15:15:09.330
2012-03-08 15:15:09.457
2012-03-23 13:06:19.913
2012-03-23 13:06:20.980
2012-03-23 13:06:21.440
2012-03-23 13:06:21.480
2012-03-23 13:06:21.550
2012-03-23 13:06:21.567
4

1 回答 1

1

您不清楚粒度,但您可以根据需要调整日期部分。您还应该告诉您的同事有关 Internet 的信息。:-)

;WITH x AS 
(
  SELECT col, rn = ROW_NUMBER() OVER (ORDER BY col)
  FROM dbo.table
)
SELECT x.col, 
  s = DATEDIFF(SECOND, x2.col, x.col),
  m = DATEDIFF(MINUTE, x2.col, x.col),
  d = DATEDIFF(DAY,    x2.col, x.col)   
FROM x LEFT OUTER JOIN x AS x2
ON x.rn = x2.rn + 1;

结果:

col                      s        m        d
-----------------------  -------  -------  ----
2012-03-08 15:08:02.260  NULL     NULL     NULL
2012-03-08 15:08:07.180  5        0        0
2012-03-08 15:15:09.220  422      7        0
2012-03-08 15:15:09.330  0        0        0
2012-03-08 15:15:09.457  0        0        0
2012-03-23 13:06:19.913  1288270  21471    15
2012-03-23 13:06:20.980  1        0        0
2012-03-23 13:06:21.440  1        0        0
2012-03-23 13:06:21.480  0        0        0
2012-03-23 13:06:21.550  0        0        0
2012-03-23 13:06:21.567  0        0        0
于 2012-07-06T17:25:18.957 回答