title: Sql Order By ... using `Case When` for different Ascending, Descending, and Custom Orders
body: <p>I want to sort product by discount on certain condition </p>
  
  <pre><code>ORDER BY 
      CASE WHEN @OrderBy = 0
      THEN table.id END ASC,
      CASE WHEN @Orderby = 2
      THEN table.id END ASC,
  </code></pre>
  
  <p>I want to do something like below as I don't have discount column in table</p>
  
  <pre><code>CASE WHEN @OrderBy = 4
  THEN (100-((table.price/table.oldprice)*100) as discount END ASC
  </code></pre>
  
  <p>but it throws an error - how can I sort by discount?</p>