title: In SQL, group user actions by first-time or recurring
body: <p>Imagine a sequence of actions. Each action is of a certain type.</p>
  
  <p>Grouping by a given time-frame (e.g. day), how many of these actions happened for the first time, and how many were recurring?</p>
  
  <p><strong>Example Input:</strong></p>
  
  <pre><code>+-----------+-------------+-------------------+
  | user_id   | action_type |     timestamp     |
  +-----------+-------------+-------------------+
  |     5     |    play     |  2014-02-02 00:55 |
  |     2     |    sleep    |  2014-02-02 00:52 |
  |     5     |    play     |  2014-02-02 00:42 |
  |     5     |    eat      |  2014-02-02 00:31 |
  |     3     |    eat      |  2014-02-02 00:19 |
  |     2     |    eat      |  2014-02-01 23:52 |
  |     3     |    play     |  2014-02-01 23:50 |
  |     2     |    play     |  2014-02-01 23:48 |
  +-----------+-------------+-------------------+
  </code></pre>
  
  <p><strong>Example Output</strong></p>
  
  <pre><code>+------------+------------+-------------+
  | first_time | recurring  |     day     |
  +------------+------------+-------------+
  |      4     |      1     |  2014-02-02 |
  |      3     |      0     |  2014-02-01 |
  +------------+------------+-------------+
  </code></pre>
  
  <p><strong>Explanation</strong></p>
  
  <p>On 2014-02-02, users 2, 3, and 5 performed various different actions. There were 4 instances were the users performed an action for the first time; in one case the user 5 repeated the action 'play'.</p>