获得约会结果的总和

时间:2010-03-24 14:14:27

标签: sql sql-server sql-server-2005

我有一个SQL语句(MS SQL Server 2005),可以简单计算几条记录中日期的差异。我也希望返回DATEDIFFs的总数/总和。

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

如何从myTotal获取SUM?这就是我想要回归的全部。

提前致谢

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

使用SUM聚合:

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