title: Can you GROUP BY with a CASE WHEN THEN alias name?
body: <p>I have a SELECT statement being calculated from a CASE WHEN THEN state (or could use multiple IF statements) aliased as 'Length', and I need to correctly GROUP the results together. The SELECT seems to be working, but the group groups them wrong. Here is my statement:</p>
  
  <pre><code>SELECT CASE 
      WHEN DATEDIFF(o.EndDate, o.StartDate) &lt; 30 THEN '&lt;1 Month'
      WHEN DATEDIFF(o.EndDate, o.StartDate) &lt; 90 THEN '1 - 2 Months'
      WHEN DATEDIFF(o.EndDate, o.StartDate) &lt; 210 THEN '3 - 4 Months'
      ELSE '&gt;4 Months' END AS 'Length', 
      COUNT(DISTINCT(person.ID)) AS 'COUNT'
  FROM person
      INNER JOIN opportunity AS o
      INNER JOIN Organization AS org
      ON person.EntityID = o.id 
          AND O.OrganizationID = Org.ID
  WHERE person.TitleID = 2
      AND o.bID = 1
  GROUP BY 'Length'
  ORDER BY 'Length' ASC;
  </code></pre>
  
  <p>This groups all results into '3 - 4 Months' which isn't right..</p>