34,593
社区成员
发帖
与我相关
我的任务
分享
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了,
所以聚合函数运算就没有问题
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.代码
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.代码