title: Use CASE statement with SUM function in SQL Server
body: <p>I am working on SQL Server 2008 R2. I am trying to get the sum.</p>
  
  <p>This is my query</p>
  
  <pre><code>select  
    SUM(
      case 
        when sec.SecurityTypeID =  2 then SUM(quantity)*(sec.AnnualIncomeRate/100) 
        when sec.SecurityTypeID = 5 then 0 
        when sec.SecurityTypeID = 11 then SUM(quantity)*sec.AnnualIncomeRate    
        else SUM(quantity)*sec.AnnualIncomeRate   
      end
    ) AS ProjectedIncome 
  from Transactions as t
  </code></pre>
  
  <p>When I execute it give me following error.</p>
  
  <blockquote>
    <p>Msg 130, Level 15, State 1, Line 3<br>
    Cannot perform an aggregate function on an expression containing an aggregate or a sub query.</p>
  </blockquote>
  
  <p>I know I am using sum function with case clause. But I need to find sum with this case statement.</p>