title: Trying to sum top 1 of subquery
body: <p>Here is a small snippet of my SELECT statement:</p>
  
  <pre><code>SELECT CASE WHEN I.IsSimCheckout=1 THEN 
      SUM(DISTINCT SalePrice) ELSE SUM(SalePrice) END 
  FROM InvoiceDetail TD WHERE TD.InvoiceID = D.InvoiceID
  </code></pre>
  
  <p>What I am trying to do is this:</p>
  
  <pre><code>SELECT CASE WHEN I.IsSimCheckout=1 THEN 
      SUM(TOP 1 SalePrice ORDER BY SalePrice) + SUM(TOP 1 SalePrice ORDER BY SalePrice DESC) ELSE SUM(SalePrice) END 
  FROM InvoiceDetail TD WHERE TD.InvoiceID = D.InvoiceID
  </code></pre>
  
  <p>Obviously, I know the latter SQL won't compile. But basically, what I'm trying to do is, if I.IsSimCheckout is equal to 1, then I want to add the highest saleprice and the lowest saleprice together, rather then summing all of them together. Hopefully what I am trying to do makes sense and is clear.</p>
  
  <p>I'm using SQL Server 2008.</p>