title: Counting readmissions in postgresql
body: <p>I have a table containing data for a prison facility, of the following format:</p>
  
  <pre><code>Prisoner_id   admission date   discharge date
  ---------------------------------------------------
  1325              06/13/2014         09/13/2014
  1266             05/01/2014          07/02/2014
  1325              02/21/2015          07/23/2015
  1471             02/26/2014          04/20/2014
  1266             10/19/2014           12/22/2014
  1325             10/09/2015           11/10/2015
  </code></pre>
  
  <p>I need to count the number of readmissions of each prisoner; that is, how many times each prisoner has been admitted again to the facility, such that the difference between his admission date (date he entered) the last time he entered the facility and his discharge date (date he was let go) the time before the last is less than 60 days.</p>
  
  <p>This means that if the same prisoner has been admitted 2 times, we count this as 1 readmission if the difference between his admission date of the second time and his discharge date of the first time is less than 60 days.</p>
  
  <p>Moreover, if a prisoner has been admitted 3 times, we count this as 2 readmissions if the difference between his discharge date the third time and his admission date the second time AND the difference between his discharge date the second time and his admission date the first time are both less than 60 days. If one of them is less than 60 days but the other is not, count as 1 readmission. If none of them is less than 60 days, count as zero readmissions.</p>
  
  <p>How can I do this in SQL or PostgreSQL? Your help is really appreciated.</p>