在下面的查询中,我需要根据条件在表格中的(HH:MM:SS)中获取两个日期时间的持续时间,并且需要对总持续时间求和.in(hh:mm:ss) 执行查询时返回错误为:
子查询返回了多个值。当 子查询遵循=,!=,<,<=,>,> =,或当子查询用作 表达式。
在下面提到的查询中
SELECT CONVERT(TIME, DATEADD(s, SUM(( DATEPART(hh, (B.DateAndTime - A.DateAndTime)) * 3600 ) + ( DATEPART(mi, (B.DateAndTime - A.DateAndTime)) * 60 ) + DATEPART(ss, (B.DateAndTime - A.DateAndTime))), 0)) AS total_time
FROM (SELECT DateAndTime, ROW_NUMBER() OVER (ORDER BY DateAndTime ASC) AS S FROM TableName WHERE Marker='S' and TagIndex=2 AND a.Val=Val) AS A
INNER JOIN
(SELECT DateAndTime, ROW_NUMBER() OVER (ORDER BY DateAndTime ASC) AS E FROM TableName WHERE Marker='E' and TagIndex=2 AND a.Val=Val) AS B
ON B.E=A.S) AS Duration
FROM TableName AS a
Left join
TableName a1 on a.Val=a1.Val
Left join
TableName b1 on a.DateAndTime=b1.DateAndTime
Left join
TableName b2 on a.DateAndTime=b2.DateAndTime
Left join
TableName b3 on a.DateAndTime=b3.DateAndTime
where a.TagIndex=2 and a.Marker='S' and a1.TagIndex=2 and a1.Marker='E' and b1.TagIndex=1 and b2.TagIndex=4 and b3.TagIndex=0 and b3.Marker='S'
group by a.Val
DateAndTime Millitm TagIndex Val Status Marker
2018-11-22 13:50:23.000 200 0 3000 S S
2018-11-23 14:50:23.000 200 3 20 S E
2019-01-13 09:43:00.000 290 0 3000 S S
2019-01-14 10:43:00.000 290 3 432 S E
D1=E-S
D2=E-S
--------
DURATION=D1+D2
数据类型是时间,这就是为什么要达到23:59小时的持续时间。如果差异小时更多,那么它的计算得出的时间也只有23:59小时?
答案 0 :(得分:0)
这确实是评论,但格式可以作为答案更容易理解。
根据SQL Syntax Check Online,您的查询有错误:
您的SQL语法有错误;似乎错误就在附近:'DATEADD(s,SUM((DATEPART(hh,(B.DateAndTime-A'在第4行
这是由 SQL语法在线检查
创建的格式化查询
第4行是DATEADD(
SELECT
CONVERT(
TIME,
DATEADD(
s,
SUM(
(
DATEPART(hh, (B.DateAndTime - A.DateAndTime)) * 3600
) + (
DATEPART(mi, (B.DateAndTime - A.DateAndTime)) * 60
) + DATEPART(ss, (B.DateAndTime - A.DateAndTime))
),
0
)
) AS total_time
FROM
(
SELECT
DateAndTime,
ROW_NUMBER() OVER (
ORDER BY
DateAndTime ASC
) AS S
FROM
TableName
WHERE
Marker = 'S'
and TagIndex = 2
AND a.Val = Val
) AS A
INNER JOIN (
SELECT
DateAndTime,
ROW_NUMBER() OVER (
ORDER BY
DateAndTime ASC
) AS E
FROM
TableName
WHERE
Marker = 'E'
and TagIndex = 2
AND a.Val = Val
) AS B ON B.E = A.S
) AS Duration
FROM
TableName AS a
Left join TableName a1 on a.Val = a1.Val
Left join TableName b1 on a.DateAndTime = b1.DateAndTime
Left join TableName b2 on a.DateAndTime = b2.DateAndTime
Left join TableName b3 on a.DateAndTime = b3.DateAndTime
where
a.TagIndex = 2
and a.Marker = 'S'
and a1.TagIndex = 2
and a1.Marker = 'E'
and b1.TagIndex = 1
and b2.TagIndex = 4
and b3.TagIndex = 0
and b3.Marker = 'S'
group by
a.Val