title: Finding free appointment slots from appointment table Postgresql Rails
body: <p>I have an app in ruby on rails that has Appointments which belong to Providers and I am looking to create a query that finds a provider's gaps in appointments Appointments are stored as a start and finish both which are datetimes . Ideally I would call Provider.free_appointment_slots(length,start,finish) where length is the length of the appointment required and start and finish are bounds of the search ie a particular week or day.</p>
  
  <p>I think it should be somewhat similar to these... <a href="https://stackoverflow.com/questions/37674964/how-to-return-all-the-free-periods-of-time-from-an-appointment-set">How to return all the free periods of time from an appointment set</a>
  <a href="https://stackoverflow.com/questions/11262260/finding-free-blocks-of-time-in-mysql-and-php">Finding free blocks of time in mysql and php?</a></p>
  
  <p>I have an appointments table with data like:</p>
  
  <pre><code>ID    |       Start       |       Finish       | ProviderID
  1       2016-11-12 09:00    2016-11-12 09:30        1
  1       2016-11-12 10:00    2016-11-12 10:30        1
  1       2016-11-12 11:30    2016-11-12 12:00        1
  </code></pre>
  
  <p>etc</p>
  
  <p>I would like to be able to provide a start and end time to search over and an appointment length and my query return the available appointment times.. for example If I was looking for appointments between 2016-11-12 08:30 and 2016-11-12 12:30 with an appointment length of 30min</p>
  
  <p>I would receive results such as:</p>
  
  <pre><code>     Start       |     Finish
  2016-11-12 08:30   2016-11-12 09:00
  2016-11-12 09:30   2016-11-12 10:00
  2016-11-12 10:30   2016-11-12 11:00
  2016-11-12 11:00   2016-11-12 11:30
  2016-11-12 12:00   2016-11-12 12:30
  </code></pre>
  
  <p>an additional great feature would be to be able to provide multiple search periods such as over different days</p>
  
  <p>I hope this is clear any help would be much appreciated</p>