title: Case in nested select
body: <p>I'm having trouble getting a CASE statement to work in a nested select. I think I'm close but I can't quite get the syntax right. So far I've tried:</p>
  
  <pre><code>SELECT l.*,
  Credit = (
  CASE WHEN ISNULL(M.POSTCODE,'') &lt;&gt; '' THEN
    sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
   ELSE
   sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4) 
   END 
  )
  FROM live l INNER JOIN master m on m.ClientID = L.ClientID 
  WHERE ClientID = 12345
  </code></pre>
  
  <p>And also:</p>
  
  <pre><code>SELECT l.*,
  (SELECT
  CASE WHEN ISNULL(M.POSTCODE,'') &lt;&gt; '' THEN
    sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
   ELSE
   sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4) 
   END 
  ) AS Credit
  FROM live l INNER JOIN master m on m.ClientID = L.ClientID 
  WHERE ClientID = 12345
  </code></pre>
  
  <p>Which seems to match the MSDN examples at <a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms181765.aspx</a> . Am I missing something?</p>