2014-06-20 119 views
0

我正在尝试计算SAS EG中两个时间列之间的差异。我正在编写一个SQL查询来做到这一点。但我得到不正确的答案。这里是我的查询:SQL中时间列之间的差异


select 
Time_DP, Time_AR, (Time_DP-time_AR) as timediff from table1 
------- 

这是我得到

Time_DP Time_AR timediff 
23:00:00 20:31:00 22900 
00:53:00 23:59:00 -230600 

这些都只是两行,我得到的例子。我猜测他的timdiff值是以秒为单位的。如果是这样,他们应该是第一排149*60秒,即8940

我还需要在hh:mm format02:29

我已经试过这科拉姆使用下述功能,没有一次成功:

--DATEDIFF(hour, Time_DP,time_AR) AS timediff 
(Error: Hour is not a column of an inserted table. SQL code = -206, SQL state=42703) 

--TIMEDIFF(CAST(AS TIME), CAST(time_AR AS TIME)) AS TIMIFF 
(Error: No function by the name timediff having compatible arguments was found in the current path. sql code=-440, sql state=42284) 

--DATEADD(SECOND, - DATEDIFF(SECOND, time_AR, Time_DP),) as timediff, 
(ERROR: Second is not a column of an inserted table, updated table or any table identified in a from clause.SQL code = -206, SQL state=42703) 

--Convert(time(0),(Convert(datetime,Time_DP) - Convert(datetime,Time_AR)),8) as timediff 
(ERROR: Datetime is not a column of an inserted table, updated table or any table identified in a from clause.SQL code = -206, SQL state=42703) 

--DATEDIFF(HOUR,Time_AR,Time_DP) AS timediff 
(ERROR: Hour is not a column of an inserted table, updated table or any table identified in a from clause.SQL code = -206, SQL state=42703) 

感谢

+2

错误的** mysql **标签应该从这个问题中删除。它应该被替换为一个指向正在使用的_actual_ RDBMS的标记(Oracle,Postgres,SQL Server等) – spencer7593

+0

是@ spencer7593是正确的,它不是mysql和错误消息不是mysql错误消息。 –

回答

3

在DB2中的减法的结果两个TIME值是持续时间,DECIMAL的形式HHMMSS。因此,从'23:00:00'减去'20:31:00'得出22900,意思是2小时29分00秒。从较早的时间'00:53:00'减去较晚的时间'23:59:00'显然给出负值-230600。