title: Sequel -- How To Construct This Query?
body: <p>I have a <code>users</code> table, which has a one-to-many relationship with a <code>user_purchases</code> table via the foreign key <code>user_id</code>.  That is, each user can make many purchases (or may have none, in which case he will have no entries in the user_purchases table).</p>
  
  <p><code>user_purchases</code> has only one other field that is of interest here, which is <code>purchase_date</code>.  </p>
  
  <p>I am trying to write a Sequel ORM statement that will return a dataset with the following columns:</p>
  
  <ul>
  <li>user_id</li>
  <li>date of the users SECOND purchase, if it exists</li>
  </ul>
  
  <p>So users who have not made at least 2 purchases will not appear in this dataset.  What is the best way to write this Sequel statement?</p>
  
  <p><strong>Please note I am looking for a dataset with ALL users returned who have >= 2 purchases</strong></p>
  
  <p>Thanks!</p>
  
  <h2>EDIT FOR CLARITY</h2>
  
  <p>Here is a similar statement I wrote to get users and their first purchase date (as opposed to 2nd purchase date, which I am asking for help with in the current post):</p>
  
  <pre class="lang-rb prettyprint-override"><code>   DB[:users].join(:user_purchases, :user_id =&gt; :id)
                .select{[:user_id, min(:purchase_date)]}
                .group(:user_id)
  </code></pre>