获取 datediff 结果的总和 [英] Getting the sum of a datediff result

查看:20
本文介绍了获取 datediff 结果的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a SQL statement (MS SQL Server 2005) that does a simple calculation of the differences in dates on a few records. I want to return the total/sum of the DATEDIFFs too.

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

如何从 myTotal 获取 SUM?这就是我想要返回的全部内容.

How do I get the SUM from myTotal? That is all I want to return.

提前致谢

推荐答案

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

If you include any other columns, you'll need to also include a GROUP BY clause

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

这篇关于获取 datediff 结果的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆