title: GROUP BY function cancels DISTINCT
body: <p>I have an SQL query that joins several tables that produces duplicates on two of the rows. I use the <code>DISTINCT</code> keyword to eliminate duplicates:</p>
  
  <pre><code>SELECT DISTINCT
            o.day as day,
            g.id AS id,
            g.name AS name,
            o.num AS num,
            o.version as version
          FROM
            table_one o
            INNER JOIN table_two t ON
              o.ID = t.ID
            INNER JOIN table_three g ON
              t.ID = g.ID
            INNER JOIN table_four gs ON
              g.ID = gs.ID
              AND
            INNER JOIN table_five s ON
              gs.ID = s.ID
            INNER JOIN table_six z ON
              s.ID = z.ID
            INNER JOIN table_seven bg ON
              bg.ID = g.ID;
  </code></pre>
  
  <p>This returns the two rows like I would want, that otherwise I would see duplicates if I did not use <code>DISTINCT</code>:</p>
  
  <pre><code>1/2/19, 5, first, 25, 1
  1/5/19, 7, second, 20, 1
  </code></pre>
  
  <p>If I remove <code>DISTINCT</code>, then the two rows are duplicated, giving me four rows:</p>
  
  <pre><code>1/2/19, 5, first, 25, 1
  1/2/19, 5, first, 25, 1
  1/5/19, 7, second, 20, 1
  1/5/19, 7, second, 20, 1
  </code></pre>
  
  <p>So my end goal is to use a <code>GROUP BY</code> function so that I can add up my <code>o.num</code> field and group them by the rest of the fields. If I add a <code>GROUP BY</code> function to the above query like so:</p>
  
  <pre><code>SELECT DISTINCT
            o.day as day,
            g.id AS id,
            g.name AS name,
            SUM(o.num) AS num,
            o.version as version
          FROM
            table_one o
            INNER JOIN table_two t ON
              o.ID = t.ID
            INNER JOIN table_three g ON
              t.ID = g.ID
            INNER JOIN table_four gs ON
              g.ID = gs.ID
              AND
            INNER JOIN table_five s ON
              gs.ID = s.ID
            INNER JOIN table_six z ON
              s.ID = z.ID
            INNER JOIN table_seven bg ON
              bg.ID = g.ID
          GROUP BY
            o.day as day,
            g.id AS id,
            g.name AS name,
            o.version as version;
  </code></pre>
  
  <p>I get two rows back, but the <code>o.num</code> amount is doubled (essentially performing  <code>GROUP BY</code> without <code>DISTINCT</code>:</p>
  
  <pre><code>1/2/19, 5, first, 50, 1
  1/5/19, 7, second, 40, 1
  </code></pre>
  
  <p>NOTE: You may be wondering why I'm trying to use a <code>GROUP BY</code> when I get the results I want with the first query. I only included the rows that are being duplicated. For some reason all other rows aren't seeing this behavior. Is there a way I can get <code>GROUP BY</code> and <code>DISTINCT</code> to work together?</p>