2010-03-24 99 views
1

我有一個SQL語句(MS SQL Server 2005),可以簡單計算幾條記錄中的日期差異。我想要返回DATEDIFF的總數/總數。獲取一個datediff結果的總和

SELECT  (DATEDIFF(day, StartDate, EndDate)+1) AS myTotal 
FROM   myTable 
WHERE  (Reason = '77000005471247') 

如何從myTotal獲取SUM?這就是我想要回報的一切。

在此先感謝

回答

3

如果包括其他列,你需要還包括GROUP BY子句

SELECT  AnotherColumn, SUM(DATEDIFF(day, StartDate, EndDate)+1) AS myTotal 
FROM   myTable 
WHERE  (Reason = '77000005471247') 
GROUP BY AnotherColumn 
+0

感謝您的,可能可能需要! – tonyyeb 2010-03-24 14:27:00

+0

這將爲'AnotherColumn'中的每個值分別給出總和。 – SLaks 2010-03-24 14:28:47

1

使用SUM合計:

SELECT  SUM(DATEDIFF(day, StartDate, EndDate)+1) AS myTotal 
FROM   myTable 
WHERE  (Reason = '77000005471247')