如何查询出时间间隔呢?
比如我有个列是时间属性的
列 我想要的
2007-08-16 15:17:36.000 0
2007-08-16 15:49:47.000 (47-17)*60+47-36
2007-08-16 16:01:52.000 (16-15)*3600+(1-49)*60+52-47
2007-08-16 16:05:49.000 类推
2007-08-16 16:18:59.000
2007-08-16 16:38:33.000
2007-08-16 16:41:08.000
2007-08-16 16:45:44.000
2007-08-16 16:48:14.000
2007-08-16 16:51:00.000
我想查出它们之间间隔多少秒
请问sql语句改怎么写?
------解决方案--------------------可以直接利用datediff
Select DateDiff(ss, '2007-08-16 15:17:36.000 ', '2007-08-16 15:49:47.000 ')
------解决方案--------------------create table tb(col datetime)
insert into tb values( '2007-08-16 15:17:36.000 ')
insert into tb values( '2007-08-16 15:49:47.000 ')
insert into tb values( '2007-08-16 16:01:52.000 ' )
insert into tb values( '2007-08-16 16:05:49.000 ')
SELECT
col=-DATEDIFF(second,(SELECT TOP 1 col FROM tb WHERE col> A.col order by col ),col)
FROM TB A
drop table tb
/*
col
-----------
1931
725
237
NULL
(所影响的行数为 4 行)
*/
------解决方案--------------------楼上正解