max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null。count 聚合函数忽略null值,如果聚合列都是null或表为空表,则返回0.
共性:Null values are ignored.
示例数据
create table dbo.ftip ( ID int)
1,当表中没有任何数据时,聚合函数的返回值
select max(ft.ID),min(ft.ID),sum(ft.ID),AVG(ft.ID),count(ft.ID) from dbo.ftip ft with(NOLOCK)
2,当表中存在数据时,聚合函数对null值得处理
2.1, 表中数据只有null
insert into dbo.ftip values(null)
select max(ft.ID),min(ft.ID),sum(ft.ID),AVG(ft.ID),count(ft.ID) from dbo.ftip ft with(NOLOCK)
2.2 表中的数据含有null,也含有非null
insert into dbo.ftip values(1)
select max(ft.ID),min(ft.ID),sum(ft.ID),AVG(ft.ID),count(ft.ID)
from dbo.ftip ft with(NOLOCK)
3,count(*)或count(0)的特殊之处,不检查null值,返回分组的总行数
select count(ft.ID),count(0),count(*) from dbo.ftip ft with(NOLOCK)
4,group by子句中,sql server 认为所有的null值是相同的,所有的null值分到一个组中。
select ft.ID,count(ft.ID),count(0),count(*),max(ft.ID),min(ft.ID),sum(ft.ID),AVG(ft.ID) from dbo.ftip ft with(NOLOCK) group by ft.ID
5,聚合函数会忽略Null值,聚合不为null的聚合列的聚合值。
insert into dbo.ftip values(2) select max(ft.ID),min(ft.ID),sum(ft.ID),AVG(ft.ID),count(ft.ID) from dbo.ftip ft with(NOLOCK)
原文:http://www.cnblogs.com/ljhdo/p/4950586.html