如何在SQL中计算Datedifferece?

时间:2015-05-30 10:17:09

标签: sql sql-server sql-server-2008

  

以下是我的sql表的一些记录: -

sno     ID      TimeRecorded        LAT      LON       SPEED
111333  1   29/05/2015 11:21:37 22.202053   88.120521   14
111332  1   29/05/2015 11:21:27 22.202178   88.12017    14
111331  1   29/05/2015 11:21:17 22.202283   88.11985    16
111329  1   29/05/2015 11:20:57 22.202516   88.118911   16
111328  1   29/05/2015 11:18:57 22.203664   88.113067   14
111327  1   29/05/2015 11:18:47 22.203754   88.112594   14
111326  1   29/05/2015 11:18:37 22.203907   88.112205   14
111325  1   29/05/2015 11:18:27 22.204015   88.111831   16
111248  1   29/05/2015 10:46:37 22.206899   88.075263   0
111247  1   29/05/2015 10:46:27 22.206928   88.075294   3
111246  1   29/05/2015 10:46:17 22.206996   88.075241   0
111245  1   29/05/2015 10:46:07 22.207046   88.075256   0
111244  1   29/05/2015 10:45:57 22.20708    88.075294   0
111243  1   29/05/2015 10:45:47 22.207067   88.075225   0
111242  1   29/05/2015 10:45:37 22.207069   88.07521    0
111241  1   29/05/2015 10:45:27 22.207044   88.075202   0
111240  1   29/05/2015 10:45:17 22.207044   88.075202   0
111239  1   29/05/2015 10:45:07 22.207044   88.075202   0
111238  1   29/05/2015 10:44:57 22.207044   88.075202   0
111237  1   29/05/2015 10:44:47 22.207044   88.075202   0
111236  1   29/05/2015 10:44:37 22.207044   88.075202   0
111235  1   29/05/2015 10:44:27 22.207096   88.075202   0
111234  1   29/05/2015 10:44:17 22.207096   88.075202   0
111233  1   29/05/2015 10:44:07 22.207096   88.075202   0
111201  1   29/05/2015 10:38:47 22.207139   88.075248   0
111200  1   29/05/2015 10:38:37 22.207139   88.075248   0
111199  1   29/05/2015 10:38:27 22.207139   88.075248   0
111198  1   29/05/2015 10:38:17 22.207139   88.075248   0
111197  1   29/05/2015 10:38:07 22.207134   88.075202   0
111196  1   29/05/2015 10:37:57 22.20722    88.075149   0
111195  1   29/05/2015 10:37:47 22.20722    88.075149   0
111194  1   29/05/2015 10:37:37 22.20722    88.075149   0
111193  1   29/05/2015 10:37:27 22.20722    88.075149   0
111192  1   29/05/2015 10:37:17 22.20722    88.075149   0
111191  1   29/05/2015 10:37:07 22.207239   88.075141   0
111190  1   29/05/2015 10:36:57 22.207233   88.075141   0
111189  1   29/05/2015 10:36:47 22.207242   88.075134   0
111188  1   29/05/2015 10:36:37 22.207242   88.075126   0
111187  1   29/05/2015 10:36:27 22.207242   88.075126   0
111186  1   29/05/2015 10:36:17 22.207242   88.075126   0
111185  1   29/05/2015 10:36:07 22.207239   88.075119   0
111184  1   29/05/2015 10:35:57 22.207115   88.07505    0
111183  1   29/05/2015 10:35:47 22.207115   88.07505    0
111182  1   29/05/2015 10:35:37 22.207115   88.07505    0
111181  1   29/05/2015 10:35:27 22.207115   88.07505    0
111180  1   29/05/2015 10:35:17 22.207115   88.07505    0
111179  1   29/05/2015 10:35:07 22.207115   88.07505    0
111178  1   29/05/2015 10:34:57 22.207115   88.07505    0
111177  1   29/05/2015 10:34:47 22.207115   88.07505    0
111176  1   29/05/2015 10:34:37 22.207115   88.07505    0
111175  1   29/05/2015 10:34:27 22.207115   88.07505    0
  

我想在ID的基础上以秒为单位计算停工时间   从时间到时间之间的速度= 0,如: -

    1- From '2015-05-29 10:56:17.000' to'2015-05-29 10:59:17.000' --160 seconds
    2-From '2015-05-29 10:31:17.000'to'2015-05-29 10:47:17.000'--960 seconds
    3-From '2015-05-29 10:31:17.000' to'2015-05-29 10:47:17.000'--1380 seconds


and so on....
  

如何以秒或分钟计算日期差异?????

1 个答案:

答案 0 :(得分:0)

如果您可以使用ID,那么您只需减去前0速度的ID与序列中最后0速度的ID之间的差异。

select (select TimeRecorded from Tablename where sno = 111286) - (select TimeRecorded from Table where sno = 111246)

要显示分钟和秒数,您可以在几秒钟内完成:

select DATEPART(mi,(select TimeRecorded from tablename where sno = 111286) - (select TimeRecorded from
tablename where sno = 111246))*60
+ DATEPART(ss,(select TimeRecorded from tablename where sno = 111286) - (select TimeRecorded from
tablename where sno = 111246))

SELECT        
(
(
select DATEDIFF(DD,(select TimeRecorded from tablename where sno = 111246),
(select TimeRecorded from tablename where sno = 111286))*86400
)
+
(
select DATEDIFF(HH,(select TimeRecorded from tablename where sno = 111246),
(select TimeRecorded from tablename where sno = 111286))*3600
)
+
(
select DATEDIFF(MI,(select TimeRecorded from tablename where sno = 111246),
(select TimeRecorded from tablename where sno = 111286))*60
)
+
(
select DATEDIFF(SS,(select TimeRecorded from tablename where sno = 111246),
(select TimeRecorded from tablename where sno = 111286))
)
)