时间文件的SUM返回0

时间:2017-05-18 10:57:17

标签: sql sql-server tsql

我正在尝试总结时间字段,但它返回0。

如果我这样使用它:

   SELECT SUM(DATEDIFF(MINUTE, '0:00:00', ServiceEnd)) FROM Location L    
   INNER JOIN Interaction I
   ON L.Id = I.IdLocation     
   GROUP BY L.Id,L.Description

它可以工作,但只要我添加这样的where子句:

   SELECT SUM(DATEDIFF(MINUTE, '0:00:00', ServiceEnd)) FROM Location L    
   INNER JOIN Interaction I
   ON L.Id = I.IdLocation
   WHERE CAST(ServiceEnd as time) = CAST('00:00:00.000' as time(3))
   GROUP BY L.Id,L.Description

每组返回0。请记住,有些行的值为00:00:00.000

没有where子句计数是这样的:

位置结果集

1091513
1939179
9562
0
2181382
164492
243632
1089518

这就是我需要的:

SELECT DATEDIFF(SECOND,servicestart,CheckinTime) abondendedTime FROM Location L INNER JOIN
     Interaction I
     ON L.Id = I.IdLocation
WHERE ServiceEnd = '00:00:00.000'
GROUP BY L.Id, L.Description,DATEDIFF(SECOND,servicestart,CheckinTime) 

但它返回600行而不是8位,如上面的结果集

所示

1 个答案:

答案 0 :(得分:0)

这又是什么回报?

SELECT L.Id, L.Description,
       SUM(DATEDIFF(MINUTE, '0:00:00', ServiceEnd))
FROM Location L INNER JOIN
     Interaction I
     ON L.Id = I.IdLocation
GROUP BY L.Id, L.Description;

你有一个WHERE条款:

WHERE CAST(ServiceEnd as time) = CAST('00:00:00.000' as time(3))

这只选择ServiceEnd为0的值。因此总和为0.

我可能还建议在几秒钟内完成计算然后转换:

SELECT L.Id, L.Description,
       SUM(DATEDIFF(SECOND, '0:00:00', ServiceEnd)) / 60.0
FROM Location L INNER JOIN
     Interaction I
     ON L.Id = I.IdLocation
GROUP BY L.Id, L.Description;