显示零而不是null

时间:2015-01-27 14:32:41

标签: sql

它显示为null,但如果为null,我想要零。我正在使用varchar。

select DISTINCT(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=1 and Potential.StageID!=8) as 'PipelinePotential',(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=6 and Potential.StageID!=8) as 'Employee PipelinePotential' , (select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=1 and Potential.StageID=8) as 'Achieved' ,
 (select Sum(isnull(cast([ExpectedRevenue] as float),0) )  from Potential where Potential.UserId=6 and Potential.StageID=8) as 'EmployeeAchieved' ,
 (select Sum(isnull(cast([target].[target] as float),0))  from Target where Target.UserId=1) as "TotalTargets " ,
  (select Sum(isnull(cast([target].[target] as float),0))  from Target where Target.UserId=6) as "EmployeeTotalTargets " 
,(Select top 1 target.Target from Target where Target.UserId=1  ORDER BY Target.TargetID desc) as "NewTarget" ,
(Select top 1 target.Target from Target where Target.UserId=6  ORDER BY Target.TargetID desc) as "EmployeeNewTarget" ,
(select Sum(isnull(cast([target].[target] as float),0)) from Target where Target.UserId=1 )-(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Target.UserId=1 and  Potential.StageID=8) as UnAchieved, 
(select Sum(isnull(cast([target].[target] as float),0)) from Target where target.UserId=6 )-(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=6 and  Potential.StageID=8) as EmployeeUnAchieved, 
(select Login.UserName from Login where Login.UserId=1) as AdminUserName ,(select Login.UserName from Login where Login.UserId=6) as EmployeeUserName 
 from potential inner join [Login] on 
potential.UserId=Login.UserId inner join [Target] on 
Target.UserId=Login.UserId  where Target.UserId=1

预期收入为零但显示为空我想要解决这个问题

1 个答案:

答案 0 :(得分:0)

你在select中使用子查询,如果没有返回记录,它将显示null,isnull对你没用。

使用select包装您的查询并在其上使用IsNull:

select
    isnull(x.PipelinePotential, 0) as PipelinePotential,
    isnull(x.[Employee PipelinePotential], 0) as 'Employee PipelinePotential'
    .... -- rest of your columns from inner query
from
(
   -- your huge query here
       select DISTINCT(
    select Sum(isnull(cast(ExpectedRevenue as float),0) ) from Potential where Potential.UserId=1 and Potential.StageID!=8) as 'PipelinePotential',
    (select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=6 and Potential.StageID!=8) as 'Employee PipelinePotential',
    (select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=1 and Potential.StageID=8) as 'Achieved',
    (select Sum(isnull(cast([ExpectedRevenue] as float),0) )  from Potential where Potential.UserId=6 and Potential.StageID=8) as 'EmployeeAchieved',
    (select Sum(isnull(cast([target].[target] as float),0))  from Target where Target.UserId=1) as "TotalTargets" ,
    (select Sum(isnull(cast([target].[target] as float),0))  from Target where Target.UserId=6) as "EmployeeTotalTargets",
    (Select top 1 target.Target from Target where Target.UserId=1  ORDER BY Target.TargetID desc) as "NewTarget",
    (Select top 1 target.Target from Target where Target.UserId=6  ORDER BY Target.TargetID desc) as "EmployeeNewTarget",
    (select Sum(isnull(cast([target].[target] as float),0)) from Target where Target.UserId=1 )-(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Target.UserId=1 and  Potential.StageID=8) as UnAchieved, 
    (select Sum(isnull(cast([target].[target] as float),0)) from Target where target.UserId=6 )-(select Sum(isnull(cast(ExpectedRevenue as float),0) )  from Potential where Potential.UserId=6 and  Potential.StageID=8) as EmployeeUnAchieved, 
    (select Login.UserName from Login where Login.UserId=1) as AdminUserName ,(select Login.UserName from Login where Login.UserId=6) as EmployeeUserName 
from
    potential inner join [Login] 
    on potential.UserId=Login.UserId 
    inner join [Target] 
    on Target.UserId=Login.UserId
where Target.UserId=1
) x