Search code examples
mysqlsqldatabasems-access-2007

SQL Sum of column based on other Column


Unfortunately My tables will be poor and I have no idea how to fix that. Essentially I have this table and I want to sum all the values on the right based on one value on the left. This is sample data but it will be 400+ records in the database. So if the numbers on the left are IDs for customers, and the numbers on the right are products purchased, how do I get a summary of products purchased per customer?

1   4
1   2
2   3
3   2
2   4
1   1
3   2
1   3

The final result would need to look like this:

1   10
2   7
3   4

Solution

  • SELECT
    user_table.userID,
    SUM(user_table.prodcut_purchase)
    FROM
    user_table
    GROUP BY
    user_table.userID