HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

MySQL: Sum if rows are equal

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rowsareequalmysqlsum

Problem

I have a table with 4 columns: Client , thickness , material and amount.

I want to make a SQL select-query to select everything, but when the client, thickness and material are the same their value of 'amount' should be added.

How would I go about creating such a query?

Solution

This is straightforward (a simple GROUP BY):

SELECT Client, thickness, material, SUM(amount) as amount
FROM your_table
GROUP BY Client, thickness, material;


Change your_table for the name of your table. If only 1 row exists of one combination of Client, thickness and material, it will select it as is.

Check the GROUP BY MySQL page for more information.

Code Snippets

SELECT Client, thickness, material, SUM(amount) as amount
FROM your_table
GROUP BY Client, thickness, material;

Context

StackExchange Database Administrators Q#83788, answer score: 3

Revisions (0)

No revisions yet.