title: Group, order, and count, by day-of-week in ActiveRecord
body: <p>I have a table with a <code>timeslot</code> column (datetime type). I would like to issue a query and have it return with data like so:</p>
  
  <pre><code>Sunday | 24
  Monday | 32
  Tuesday | 12
  Wednesday | 93
  Thursday | 22
  Friday | 66
  Saturday | 25
  </code></pre>
  
  <p>Basically, ordered by day-of-week (Sunday -> Monday) in the first column, and the <code>count(*)</code> of the rows in the second column.</p>
  
  <p>So far, I have a query like so:</p>
  
  <pre><code>order_query = "CASE
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Sunday' THEN 1
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Monday' THEN 2
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Tuesday' THEN 3
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Wednesday' THEN 4
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Thursday' THEN 5
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Friday' THEN 6
      WHEN DAYNAME(CONVERT_TZ(timeslot,'UTC','America/Toronto')) = 'Saturday' THEN 7
  END"
  
  week = ProfessionalAvailability.where.not(appointment_id: nil).group('DAYNAME(CONVERT_TZ(timeslot,"UTC","America/Toronto"))').order(order_query).count
  </code></pre>
  
  <p>Which works for my case. However, as you can see, <code>order_query</code> is a rather long string, and I wanted to know if there was a simple ActiveRecord way for me to do this.</p>
  
  <p>My DB is in MySQL.</p>
  
  <p>Many kind thanks~</p>