title: Query for customers who have made purchases on a common but discontinuous set of dates
body: <p>RDMS: PostgreSQL 9.5.3 </p>
  
  <p>I have a table ('activity') of the following form:</p>
  
  <pre><code>customerID | date           | purchaseID
  -----------------------------------------
  1          | 2016-01-01     | 1
  2          | 2016-01-01     | 2
  3          | 2016-01-01     | 3
  2          | 2016-01-02     | 4
  1          | 2016-01-03     | 5
  2          | 2016-01-03     | 6
  3          | 2016-01-03     | 7
  1          | 2016-01-04     | 8
  2          | 2016-01-04     | 9
  3          | 2016-01-05     | 10
  </code></pre>
  
  <p>From this table, I want to find all customers who have made purchases on the same dates as customerID 1. The customers purchase history needs to completely overlap with customerID 1, but not necessarily be limited to it -- extra purchases outside of the dates are fine, but should not be returned in the final results.</p>
  
  <p>The result on the above data should be:</p>
  
  <pre><code>customerID | date           | purchaseID
  -----------------------------------------
  2          | 2016-01-01     | 2
  2          | 2016-01-02     | 5
  2          | 2016-01-03     | 8
  </code></pre>
  
  <p>At the moment, I'm solving this through a loop in the application code and then dropping all NULL results, so the actual SQL is:</p>
  
  <pre><code>SELECT customerID,
         date,
         purchaseID
  FROM activity
  WHERE customerID &lt;&gt; 1
     AND date = %date%
  </code></pre>
  
  <p>where %date% is the is an iteration variable through all of the dates that customerID 1 has made purchases. This isn't an elegant solution, and extremely slow for large numbers of purchases (millions) or customers (tens of thousands). Any suggestions would be welcome. </p>
  
  <p>Thanks for reading-- </p>