SQL关于:警告: 聚合或其他 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了, 
所以聚合函数运算就没有问题

方法二:

增加SQL语句

SET ANSI_WARNINGS OFF

posted on 2019-05-29 17:30  岁月寒风  阅读(4650)  评论(0编辑  收藏  举报

导航