我有一个SQL语句(MS SQL Server 2005),可以简单计算几条记录中日期的差异。我也希望返回DATEDIFFs的总数/总和。
SELECT (DATEDIFF(day, StartDate, EndDate)+1) AS myTotal
FROM myTable
WHERE (Reason = '77000005471247')
如何从myTotal获取SUM?这就是我想要回归的全部。
提前致谢
答案 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')