SQL Server选择两个日期之间的差异

时间:2015-07-07 10:17:30

标签: sql sql-server-2008

enter image description here 亲爱,

我想在两个类型之间的DATESTAMP列中的两个日期之间获得时间差“仅”打开“& “状态改变”。 我写了这个查询,但我得到了null和错误信息

declare @StartDate datetime, @EndDate datetime
set @StartDate = (Select Datestamp from ACTIVITYM1 where TYPE='Open')
set @EndDate = (Select Datestamp from ACTIVITYM1 where TYPE='Status Change')
select convert(varchar(5),DateDiff(s, @startDate, @EndDate)/3600)+':'+convert(varchar(5),DateDiff(s, @startDate, @EndDate)%3600/60)+':'+convert(varchar(5),(DateDiff(s, @startDate, @EndDate)%60)) as [hh:mm:ss]
from ACTIVITYM1

下面是我得到的错误 enter image description here

1 个答案:

答案 0 :(得分:5)

查看错误消息:"子查询返回的值超过1 ..."。 您只需要选择一行,例如:

set @StartDate = (Select Datestamp from ACTIVITYM1 where TYPE='Open' AND THENUMBER = '001A1419')

或设置查询限制......