我们在我们的数据库中有一个表格,各种系统报告时间戳和值。该表被称为数据表。 (我知道,我没有把它命名。)计算记录之间的日期差异
因此,我可以完成任务并查找特定系统报告之间的时间差异。
不知道怎么回事,去了解这一点,我创建了一个临时表,例如:
CREATE TABLE #Readings(
id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
timestamp DATETIME
)
然后我插入所有读数的特定系统:
INSERT INTO #Readings (timestamp)
SELECT ReadingAt
FROM data
WHERE SenId = 3
ORDER BY ReadingAt
最后,我运行我的查询:
select r1.id, r1.timestamp, datediff(second, r1.timestamp, (select r2.timestamp
from #Readings r2 where r2.id = (r1.id - 1)))
from #Readings r1
where id > 1
但这返回:
101 2011-07-14 04:44:05.443 <null>
102 2011-07-14 04:46:05.443 -120
103 2011-07-14 04:48:05.447 -120
104 2011-07-14 04:50:05.447 -120
105 2011-07-14 04:52:05.447 -120
106 2011-07-14 04:54:05.45 -120
107 2011-07-14 04:56:05.45 -120
108 2011-07-14 04:58:05.45 -120
109 2011-07-14 05:00:05.45 -120
110 2011-07-14 05:02:05.453 -120
所以我做了以下内容:
select r1.id, r1.timestamp, (select r2.timestamp from #Readings r2 where r2.id = (r1.id - 1))
from #Readings r1
where id > 1
其返回正确的日期。
所以,我想知道,我该怎么做?
谢谢。
你得到的结果看起来合理的,我 - 如果你想正值,只是改变了第二顺序和第三个操作数。你期望得到什么? – 2011-12-28 20:39:35
无论时间范围如何,结果总是120秒。这就是为什么他们不合理。 – Thom 2011-12-28 20:43:05
这其实是正确的答案。哎呦。感谢您指出。 – Thom 2011-12-28 20:44:27