title: MySQL COUNT(DISTINCT) giving wrong values with GROUP BY
body: <p>I have a table that contains custom user analytics data. I was able to pull the number of unique users with a query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT COUNT(DISTINCT(user_id)) AS 'unique_users'
  FROM `events`
  WHERE client_id = 123
  </code></pre>
  
  <p>And this will return <code>16728</code></p>
  
  <p>This table also has a column of type <code>DATETIME</code> that I would like to group the counts by. However, if I add a <code>GROUP BY</code> to the end of it, everything groups properly it seems except the totals don't match. My new query is this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT COUNT(DISTINCT(user_id)) AS 'unique_users', DATE(server_stamp) AS 'date'
  FROM `events`
  WHERE client_id = 123
  GROUP BY DATE(server_stamp)
  </code></pre>
  
  <p>Now I get the following values:</p>
  
  <pre><code>|-----------------------------|
  | unique_users  | date        |
  |---------------|-------------|
  | 2650          | 2019-08-26  |
  | 3486          | 2019-08-27  |
  | 3475          | 2019-08-28  |
  | 3631          | 2019-08-29  |
  | 3492          | 2019-08-30  |
  |-----------------------------|
  </code></pre>
  
  <p>Totaling to <code>16734</code>. I tried using a sub query to get the distinct users then count and group in the main query but no luck there. Any help in this would be greatly appreciated. Let me know if there is further information to help diagnosis.</p>