title: INSERT a column into a table with SUM and GROUP BY
body: <p>What is the best approach to insert a calculated column grouped by an ID into another column where the ID matches?</p>
  
  <p>I have this query:</p>
  
  <pre><code>SELECT DISTINCT cs.customerNumber, cs.totalOrderCost as paymentDue, 
         SUM(p.amount) as paymentMade 
  FROM customerSales cs LEFT OUTER JOIN payments p 
            ON cs.customerNumber = p.customerNumber 
  GROUP BY cs.customerNumber;
  </code></pre>
  
  <p>which returns customerNumber, paymentDue, paymentMade, calculated correctly.</p>
  
  <p>In the table customerSales, there are two columns: customerNumber and totalOrderCost. I want to add the "paymentMade" column (summed from the payments column and grouped by customerNumber) into the customerSales table.</p>
  
  <p>This approach:</p>
  
  <pre><code>ALTER TABLE customerSales 
      ADD COLUMN paymentMade DECIMAL; 
  INSERT INTO customerSales(paymentMade) 
  SELECT SUM(p.amount) as paymentMade 
  FROM payments p 
  GROUP BY p.customerNumber; 
  </code></pre>
  
  <p>gives the error "Field customerNumber doesn't have a default value". </p>
  
  <p>Any help would be appreciated. Thank you!</p>