4

我偶然发现了一个关于哪些聚合函数适用于 DATE 类型列的问题(在测试中)。因此,据我了解, COUNT 只会计算行数,而 MIN 和 MAX 返回最早/最晚日期。但是,我对 SUM 和 AVG 函数有点困惑。他们是否会将 DATE 值转换为整数并计算这些整数的总和/平均值?还是我在这里错了?无论如何,这种行为在所有 SQL 实现中是否一致?提前致谢。

4

1 回答 1

5

In MS SQL Server You cannot call the SUM operator on datetime types, nor can you call the AVG operator.

MSDN lists the return types of the SUM operator here:
http://msdn.microsoft.com/en-us/library/ms187810.aspx

It is also a valid reference for the types on which you may invoke the SUM operator.

EDIT: In response to your comment, you may use a site like sqlfiddle to test various implementations

http://www.sqlfiddle.com/#!3/22cee/1

Considering the backend storage of datetime is not standardized, I contend that it should not be depended upon for any database to return SUM or AVG results in predictable ways.... better not to do it at all...

于 2012-06-14T19:45:45.737 回答