2

I need to return a zero if the subquery returns no rows. I have tried multiple suggestions found here on SO, to no avail, along with a case statement and coalesce.

My end goal is to sum the resulting subquery results together, but since the 'misc' query returns no rows, the TTL field is blank.

Where am I going wrong, and how do I correct?

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

Currently returns:

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

I have spent the last three hours trying to return as below:

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 
0

1 Answer 1

6

Use 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 takes the first non-NULL value in the values provided. If misc.amt is NULL, it would default it to the second value, 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.