计算时差之和

时间:2017-05-17 02:33:58

标签: sql sql-server

我有这样的查询:

<div id="app">
     <div v-html="data.template"></div>
</div>

并且当我仅运行此部分时返回null。

DECLARE @AbandonTime int
SET @AbandonTime += (SELECT SUM(DATEDIFF(second,CheckinTime,ServiceStart)) FROM Interaction
WHERE ServiceEnd = '00:00:00.000')
SELECT @AbandonTime

它返回1378条记录。如何计算时差之和?

1 个答案:

答案 0 :(得分:1)

您需要初始化该值,因为NULL加上任何值仍为NULL

DECLARE @AbandonTime int;
SET @AbandonTime = 0;

SET @AbandonTime += (SELECT SUM(DATEDIFF(second, CheckinTime, ServiceStart))
                     FROM Interaction
                     WHERE ServiceEnd = '00:00:00.000'
                    );
SELECT @AbandonTime;

当然,您可以将其简化为:

DECLARE @AbandonTime int;

SELECT @AbandonTim = SUM(DATEDIFF(second, CheckinTime, ServiceStart))
FROM Interaction
WHERE ServiceEnd = '00:00:00.000';

SELECT @AbandonTime;