title: SQL query to find the cancellation rate of requests made between two dates using WITH
body: <p><strong>I'm trying to understand the right way to divide the count sums from two queries.</strong></p>
  <p>I'm teaching myself sql and practising it on line.<br />
  Question:</p>
  <p>Write a SQL query to find the cancellation rate of requests made between 2017-08-01 and 2017-08-03. The cancellation rate is given by dividing the number of cancelled requests by the total number of rides each day. The result table should have 2 Columns, namely Day that shows each day and Cancellation Rate that provides the cancellation rate of that day.<br />
  Table is:<br />
  <a href="https://i.stack.imgur.com/UOY5u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UOY5u.png" alt="enter image description here" /></a>
  What I tried was:</p>
  <ol>
  <li>count cancelled ride rates per date</li>
  <li>count all ride requests per date</li>
  <li>divide both the counts per date</li>
  </ol>
  <pre><code>     with  
      cancelled_rides as 
      (select count(*) cancel_count, status, Request_id 
      from TRIPS 
      where status = 'cncld_driver' 
      group by state, Request_id)
       
      all_rides as ( 
      select count(*) day_count, status, Request_id 
      from TRIPS 
      group by state, Request_id) ,
       
      select cancelled_rides.Request_id as DAY, 
      (cancelled_rides.cancel_count/all_rides.day_count) as 'Cancellation Rate' 
      FROM cancelled_rides, all_rides;
  </code></pre>
  <p>Does this look right?  Note I purposefully ignored including date ranges as the table has only limited entries.</p>