SQL Server:如果子查询结果为IS NULL,则返回零所有行

时间:2017-02-10 21:04:04

标签: sql-server subquery

如果子查询没有返回任何行,我需要返回零。我已经在SO上尝试了多个建议,但无效,还有案例陈述和合并。

我的最终目标是将得到的子查询结果汇总在一起,但是因为' misc'查询不返回任何行,TTL字段为空。

我哪里出错了,我该如何纠正?

Select 
    s.STID,
    rent.amt As RENT,
    misc.amt As MISC,
    rent.amt + misc.amt As TTL
From 
    (Select 
         s.STID
     From 
         Stores s
     Where 
         s.STID Not In (7, 999) ) As s
Left Join 
    (Select 
         Income.STID,
         Sum(Case When Income.Amount Is Null Then 0 Else Income.Amount End) As amt
     From 
         Income
     Inner Join 
         IncomeTypes On IncomeTypes.IncTypeID = Income.IncTypeID
     Inner Join 
         IncomeSection On IncomeSection.IncSecID = IncomeTypes.IncSecID
     Where 
         Income.IncomeDate = dbo.getdateparam(92, 999) And
         IncomeTypes.IncTypeID In (1, 2)
     Group By 
         Income.STID) As rent on s.STID = rent.STID
Left Join 
    (Select 
         Income.STID,
         Coalesce(Sum(Income.Amount), 0) As amt
     From 
         Income
     Inner Join 
         IncomeTypes On IncomeTypes.IncTypeID = Income.IncTypeID
     Inner Join 
         IncomeSection On IncomeSection.IncSecID = IncomeTypes.IncSecID
     Where 
         Income.IncomeDate = dbo.getdateparam(92, 999) And
         IncomeTypes.IncTypeID In (20, 21, 22)
     Group By 
         Income.STID) As misc On s.STID = misc.STID
Order By 
    s.STID

目前返回:

STID    RENT      MISC    TTL
1       1234.56 
2       1234.56 
3       1234.56 
4       1234.56 
5       1234.56 

我花了最后三个小时试图返回如下:

STID    RENT      MISC    TTL
1       1234.56   0       1234.56
2       1234.56   0       1234.56 
3       1234.56   0       1234.56 
4       1234.56   0       1234.56 
5       1234.56   0       1234.56 

1 个答案:

答案 0 :(得分:3)

使用COALESCE

Select  s.STID
      , rent.amt As RENT
      , Coalesce(misc.amt, 0) As MISC
      , rent.amt + Coalesce(misc.amt, 0) As TTL
From    (Select s.STID
         From   Stores s
         Where  s.STID Not In (7, 999)
        ) As s
Left Join (Select   Income.STID
                  , Sum(Case When Income.Amount Is Null Then 0
                             Else Income.Amount
                        End) As amt
           From     Income
           Inner Join IncomeTypes
                    On IncomeTypes.IncTypeID = Income.IncTypeID
           Inner Join IncomeSection
                    On IncomeSection.IncSecID = IncomeTypes.IncSecID
           Where    Income.IncomeDate = dbo.getdateparam(92, 999)
                    And IncomeTypes.IncTypeID In (1, 2)
           Group By Income.STID
          ) As rent
        On s.STID = rent.STID
Left Join (Select   Income.STID
                  , Coalesce(Sum(Income.Amount), 0) As amt
           From     Income
           Inner Join IncomeTypes
                    On IncomeTypes.IncTypeID = Income.IncTypeID
           Inner Join IncomeSection
                    On IncomeSection.IncSecID = IncomeTypes.IncSecID
           Where    Income.IncomeDate = dbo.getdateparam(92, 999)
                    And IncomeTypes.IncTypeID In (20, 21, 22)
           Group By Income.STID
          ) As misc
        On s.STID = misc.STID
Order By s.STID;

COALESCE获取所提供值中的第一个非NULL值。如果misc.amtNULL,则会将其默认为第二个值0