title: Combining two queries to add additional column
body: <p>I have two SQL queries that I want to combine into one. The 2nd should add an additional column to the 1st.</p>
  
  <p>Query #1:</p>
  
  <pre><code>SELECT 
      T3.AcctCode, T3.AcctName,
      SUM(CASE WHEN T0.TaxDate BETWEEN '2015-12-01' AND '2015-12-31'
               THEN T1.Debit - T1.Credit 
               ELSE 0 END)* -1 AS [Balance],
      SUM(T1.Debit - T1.Credit)* -1  AS [YTD Balance],
      (CASE WHEN T3.AcctCode BETWEEN '611110' AND '612000'
         THEN 1 WHEN T3.AcctCode BETWEEN '541100' AND '541950'
         THEN 2 WHEN T3.AcctCode BETWEEN '542100' AND '542700'
         THEN 3 WHEN T3.AcctCode = '543100'
         THEN 4 WHEN T3.AcctCode BETWEEN '511100' AND '511950'
         THEN 5 WHEN T3.AcctCode BETWEEN '512100' AND '512995'
         THEN 6 WHEN T3.AcctCode = '513100'
         THEN 7 ELSE 0 END) AS [Group]
  FROM 
      OJDT T0
  INNER JOIN 
      JDT1 T1 ON T0.[TransId] = T1.[TransId]
  INNER JOIN 
      OACT T2 ON T1.[Account] = T2.[AcctCode]
  INNER JOIN 
      OACT T3 ON T3.[AcctCode] = T2.[FatherNum]
  WHERE 
      T0.TaxDate BETWEEN '2015-04-01' AND '2015-12-31'     
      AND T2.GroupMask IN(5,6)
  GROUP BY 
      T3.AcctCode, T3.AcctName
  </code></pre>
  
  <p>Sample Output</p>
  
  <p><a href="https://i.stack.imgur.com/GOzPL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GOzPL.png" alt="enter image description here"></a></p>
  
  <p>Query #2:</p>
  
  <pre><code>SELECT 
      T1.FatherNum, SUM(T0.CredLTotal) AS [Budget]
  FROM 
      OBGT T0
  LEFT JOIN 
      OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
  WHERE 
      T1.GroupMask IN (5, 6) 
      AND (T1.FatherNum BETWEEN '511100' AND '612000') 
      AND YEAR(T0.FinancYear) = 2015 
  GROUP BY 
      T1.FatherNum
  </code></pre>
  
  <p>Sample Output</p>
  
  <p><a href="https://i.stack.imgur.com/u7z7p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u7z7p.png" alt="enter image description here"></a></p>
  
  <p>I have come up with this:</p>
  
  <pre><code>SELECT 
      T3.AcctCode, T3.AcctName,
      SUM(CASE WHEN T0.TaxDate BETWEEN '2015-12-01' AND '2015-12-31'
               THEN T1.Debit - T1.Credit 
               ELSE 0 END)* -1 AS [Balance],
      SUM(T1.Debit - T1.Credit)* -1  AS [YTD Balance],
      SUM(T4.CredLTotal) AS [Budget],
      (CASE WHEN T3.AcctCode BETWEEN '611110' AND '612000'
         THEN 1 WHEN T3.AcctCode BETWEEN '541100' AND '541950'
         THEN 2 WHEN T3.AcctCode BETWEEN '542100' AND '542700'
         THEN 3 WHEN T3.AcctCode = '543100'
         THEN 4 WHEN T3.AcctCode BETWEEN '511100' AND '511950'
         THEN 5 WHEN T3.AcctCode BETWEEN '512100' AND '512995'
         THEN 6 WHEN T3.AcctCode = '513100'
         THEN 7 ELSE 0 END) AS [Group]
  FROM 
      OJDT T0
  INNER JOIN 
      JDT1 T1 ON T0.[TransId] = T1.[TransId]
  INNER JOIN 
      OACT T2 ON T1.[Account] = T2.[AcctCode]
  INNER JOIN 
      OACT T3 ON T3.[AcctCode] = T2.[FatherNum]
  LEFT JOIN 
      OBGT T4 ON (T4.[AcctCode] = T2.[AcctCode] 
              AND YEAR(T4.FinancYear) = 2015)
  WHERE 
      T0.TaxDate BETWEEN '2015-04-01' AND '2015-12-31'     
      AND T2.GroupMask IN (5, 6)     
  GROUP BY 
      T3.AcctCode, T3.AcctName, T2.FatherNum
  ORDER BY 
      T3.AcctCode
  </code></pre>
  
  <p>Sample Output</p>
  
  <p><a href="https://i.stack.imgur.com/BczQG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BczQG.png" alt="enter image description here"></a></p>
  
  <p>However, the column <code>Balance</code> shows incorrect figures, i.e. not returning the results of the 2nd query.</p>
  
  <p>How do I get <code>SUM(T4.CredLTotal) AS [Budget]</code> to sum properly?</p>