小编典典

获取datediff结果的总和

sql

我有一条SQL语句(MS SQL Server 2005),可以对一些记录上的日期差异进行简单的计算。我也想返回DATEDIFF的总数/总和。

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

如何从myTotal获取总和?这就是我要返回的全部。

提前致谢


阅读 200

收藏
2021-03-17

共1个答案

小编典典

如果包含其他任何列,则还需要包含GROUP BY子句

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