case when 表达式 is null then 0 end ,结果一直是null

iiiiiiiii_z 2018-05-17 10:52:20
case when 表达式 is null then 0 end
我这里的表达式是一个select 语句,我的想法是表达式如果查出来不为null就是自身的值(如,10,23,78...),为null是0,但是现在有值一直是null。


select d.fjmc1 as 辅具名称,d.fjbh1 as 产品编号,
sum(case when d.fjsx1='成品' and d.chaptertype='1' then 1 else 0 end ) as A目录,
sum(case when d.fjsx1='成品' and d.chaptertype='2' then 1 else 0 end ) as B目录,
sum(case when d.fjsx1='成品' then 1 else 0 end ) as 合计,
sum(case when d.fjsx1='定制品' or d.fjsx1='改制品' then 1 else 0 end ) as "定改制(件)",
(
case when (SELECT sum(convert(int,cast(b.dj1 as float)) *convert(int,cast(b.sl1 as float)) )
from dsass_zwb_fjkffwxq a INNER JOIN dsassfjfjpsdjb b on a.ida_psdj=b.id where b.fjbh1 is not null and b.chaptertype='1') is null then 0 end
) as A目录a,
(
case when (SELECT sum(convert(int,cast(b.dj1 as float)) *convert(int,cast(b.sl1 as float)) )
from dsass_zwb_fjkffwxq a INNER JOIN dsassfjfjpsdjb b on a.ida_psdj=b.id where b.fjbh1 is not null and b.chaptertype='2') is null then 0 end

) as B目录a ,
((SELECT sum(convert(int,cast(b.dj1 as float)) *convert(int,cast(b.sl1 as float)) )
from dsass_zwb_fjkffwxq a INNER JOIN dsassfjfjpsdjb b on a.ida_psdj=b.id where b.fjbh1 is not null and b.chaptertype='1')+
(SELECT sum(convert(int,cast(b.dj1 as float)) *convert(int,cast(b.sl1 as float)) )
from dsass_zwb_fjkffwxq a INNER JOIN dsassfjfjpsdjb b on a.ida_psdj=b.id where b.fjbh1 is not null and b.chaptertype='2')) as 合计

from dsass_zwb_fjkffwxq c INNER JOIN dsassfjfjpsdjb d on c.ida_psdj=d.id where d.fjbh1 is not null GROUP BY d.fjmc1,d.fjbh1
...全文
2524 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbdzjx 2018-05-18
  • 打赏
  • 举报
回复 1
case when 表达式 is null then 0 end 要改成 case when 表达式 is null then 0 else 表达式 end 或者用楼上的:isnull(表达式, 0)
卖水果的net 2018-05-17
  • 打赏
  • 举报
回复
case when 表达式 is null then 0 end 换成 isnull(表达式, 0)

22,293

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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