执行SQL语句后,警告: 聚合或其他 SET 操作消除了空值。

yiyaozjk 2010-04-03 11:20:59
update 结存 set 结存.现有库存=c.入仓数-isnull(b.出仓数量,0)
from 结存 a
join (select 物料代码,sum(入仓数) 入仓数 from 进仓 group by 物料代码) c on a.代码=c.物料代码
left join (select 代码,sum(出仓数量) 出仓数量 from 领料 group by 代码) b on a.代码=b.代码

能正确执行完上述语句,但出现了这样的警告

警告: 聚合或其他 SET 操作消除了空值。
(129 行受影响)

再打开结存表,发现后面有很多行的全是NULL的记录,即所有的字段全是NULL,
这是不是说该语句没有找到条件,而全部执行了NULL字段填充。但实际上是不可能的,每个月的进仓、出仓怎么可能都会没有呢??
...全文
3286 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
老黎 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 beirut 的回复:]
SQL code
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)
f……
[/Quote]
写的很清楚
Server只是在进行值聚合时把null做了为0的操作,提醒用户应该对null做业务上的处理,不一定是设为0
黄_瓜 2010-04-03
  • 打赏
  • 举报
回复
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了,
所以聚合函数运算就没有问题
yiyaozjk 2010-04-03
  • 打赏
  • 举报
回复

那“警告: 聚合或其他 SET 操作消除了空值。 ”

是说明出了什么问题吗?? 能不能讲解一下,出现这个警告的原因。。。

按照happyflystone的写是法是正确,结果没有出现任何警告提示,同样也是影响了129行。。谢谢
黄_瓜 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
膜拜楼上两位..
[/Quote]
fjj你的mvp发没发到手?
htl258_Tony 2010-04-03
  • 打赏
  • 举报
回复
用ISNULL函数来消空
--小F-- 2010-04-03
  • 打赏
  • 举报
回复
膜拜楼上两位..
黄_瓜 2010-04-03
  • 打赏
  • 举报
回复
update 结存 set 结存.现有库存=c.入仓数-b.出仓数量
from 结存 a
join (select 物料代码,sum(isnull(入仓数,0)) 入仓数 from 进仓 group by 物料代码) c
on a.代码=c.物料代码
left join (select 代码,sum(isnull(出仓数量,0)) 出仓数量 from 领料 group by 代码) b
on a.代码=b.代码
-狙击手- 2010-04-03
  • 打赏
  • 举报
回复
update 结存 set 结存.现有库存=c.入仓数-isnull(b.出仓数量,0)
from 结存 a
join (select 物料代码,sum(isnull(入仓数,0)) 入仓数 from 进仓 group by 物料代码) c on a.代码=c.物料代码
left join (select 代码,sum(isnull(出仓数量,0)) 出仓数量 from 领料 group by 代码) b on a.代码=b.代码
yhtapmys 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 beirut 的回复:]
SQL code
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)
f……
[/Quote]
学习~

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧