警告: 聚合或其他 SET 操作消除了空值。 是什么意思?

 

原因:


create table tb 

id int, 
num int 



insert into tb select 1,10 
insert into tb select 1,20 
insert into tb select 2,80 
insert into tb select 2,null 


select id,sum(num) 
from tb 
group by id 


id                      
----------- ----------- 
1          30 
2          80 


(所影响的行数为 2 行) 


警告: 聚合或其它 SET 操作消除了空值。 


分析:聚合函数无法对null值进行运算,所以会忽略 
这个提示仅仅是警告,就是告诉用户,null值被忽略了 
结果就是按照null为0来计算 



如果用 
select id,sum(isnull(num,0)) 
from tb 
group by id 


这样的语句,在运算之前,isnull已经把null值转换成0了, 
所以聚合函数运算就没有问题


 

posted on 2012-03-27 08:18  babyblue  阅读(1411)  评论(0)    收藏  举报