title: SQL Server - Order by name from Subquery
body: <p>Below, my second case does not work because I'm referencing the name from the subquery. What is a good solution to this problem? Also, I'm declaring the SortBy parameter... just didn't include that in the example. Thank you so much for any help!   </p>
  
  <pre><code>SELECT
   a.[PostID]
  ,a.[Title]
  ,a.[Date_Created] 
  ,(SELECT COUNT(VoteID) AS VoteCount 
       FROM [VoteTable] WHERE [PostID] = a.[PostID]) AS VoteCount
  
  FROM [PostTable] a
  INNER JOIN [Users] b
  ON a.Created_UserID = b.UserID
  WHERE a.Approved = 1
  
  ORDER BY 
  CASE @SortBy
      WHEN 1 THEN a.[Date_Created] END DESC,
  CASE @SortBy
      WHEN 2 THEN [VoteCount] END DESC
  </code></pre>