title: How to have a custom sort order for a union query in Postgres
body: <p>With a query like this (simplified for clarity):</p>
  
  <pre><code>SELECT 'East' AS name, *
  FROM events 
  WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' 
  
  UNION
  
  SELECT 'West' AS name, *
  FROM events 
  WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00'
  
  UNION
  
  SELECT 'Both' AS name, *
  FROM events 
  WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00'
  </code></pre>
  
  <p>I want to customise the order of the resulting rows. Something like:</p>
  
  <pre><code>ORDER BY name='East', name='West', name='Both'
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>ORDER BY 
      CASE
          WHEN name='East' THEN 1 
          WHEN name='West' THEN 2
          WHEN name='Both' THEN 3
          ELSE 4
      END;
  </code></pre>
  
  <p>However, Postgres complains with:</p>
  
  <pre><code>ERROR:  invalid UNION/INTERSECT/EXCEPT ORDER BY clause
  DETAIL:  Only result column names can be used, not expressions or functions.
  HINT:  Add the expression/function to every SELECT, or move the UNION into a FROM clause.
  </code></pre>
  
  <p>Do I have any alternative?</p>