title: Using SQL SUM with Case statement containing inner SELECT
body: <p>I have two tables, an <code>Orders</code> table which contains a list of a users orders and a <code>OrderShippingCosts</code> table which contains a price for shipping each item based on the <code>OrderTypeID</code> in the <code>Orders</code> table. </p>
  
  <p>I am running a query like below to calculate the total shipping costs:</p>
  
  <pre><code>SELECT
      SUM(CASE 
             WHEN OR.OrderTypeID = 1 
                THEN (SELECT CostOfShippingSmallParcel 
                      FROM OrderShippingCosts) 
                ELSE (SELECT CostOfShippingBigParcel 
                      FROM OrderShippingCosts) 
          END) AS TotalShippingCost
  FROM 
      Orders AS OR
  </code></pre>
  
  <p>But I'm getting the following error:</p>
  
  <blockquote>
    <p>Cannot perform an aggregate function on an expression containing an aggregate or a subquery</p>
  </blockquote>
  
  <p>Does anyone know what is wrong with my query?</p>