MySQL SUM
MySQL SUM
If you use the SUM() function in a SELECT statement that returns no row,
the SUM() function returns NULL, not zero.
The DISTINCT option instructs the SUM() function to calculate the sum of
only distinct values in a set.
The SUM() function ignores the NULL values in the calculation.
As you can see, the SUM() function calculates the total of 1, 1, 2, and 3. And it
ignores NULL.
Finally, use the SUM() with the DISTINCT option to calculate the total values in
the n column:
SELECT
SUM(DISTINCT n)
FROM
sum_demo;
Code language: SQL (Structured Query Language) (sql)
In this case, the SUM() with the DISTINCT option only calculates the sum of
distinct values which are 1, 2 and 3.
This example uses the SUM() function to get the total number of items of the
order details:
SELECT
SUM(quantityOrdered) SalesQuantity
FROM
orderdetails;
Code language: SQL (Structured Query Language) (sql)
The following shows the order line items of the order number 10110:
SELECT
orderNumber,
quantityOrdered,
priceEach
FROM
orderdetails
WHERE
orderNumber = 10100;
Code language: SQL (Structured Query Language) (sql)
To calculate the total for the order number 10110, you use the SUM() function
as follows:
SELECT
SUM(quantityOrdered * priceEach) orderTotal
FROM
orderdetails
WHERE
orderNumber = 10100;
Code language: SQL (Structured Query Language) (sql)
In this tutorial, the SUM() function calculates the total of the following
expression of all order line items of the order number 10110:
quantityOrdered * priceEach
Code language: SQL (Structured Query Language) (sql)
The SUM() function is often used with the GROUP BY clause to calculate the
sum for each group.
For example, you can calculate the total amount of each order by using
the SUM() function with the GROUP BY clause as shown in the following query:
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) orderTotal
FROM
orderdetails
GROUP BY
orderNumber
ORDER BY
orderTotal DESC;
Code language: SQL (Structured Query Language) (sql)
In this example:
The GROUP BY clause divides order details into groups grouped by the
order number.
The SUM() function calculates the total of each amount of each order.
You can use the SUM() function in the HAVING clause to filter the group. This
example illustrates how to select orders whose order amounts are greater
than 60,000.
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) orderTotal
FROM
orderdetails
GROUP BY
orderNumber
HAVING
SUM(quantityOrdered * priceEach) > 60000
ORDER BY
orderTotal;
Code language: SQL (Structured Query Language) (sql)
The SUM() function returns NULL if the result set is empty. Sometimes, you may
want the SUM() function to return zero instead of NULL.
In this case, you can use the COALESCE() function. The COALESCE function
accepts two arguments and returns the second argument if the first argument
is NULL; otherwise, it returns the first argument.
See the following query:
SELECT
COALESCE(SUM(quantityOrdered * priceEach), 0) result
FROM
orderdetails
WHERE
productCode = 'S1_20';
Code language: SQL (Structured Query Language) (sql)
You can use the SUM() function in a SELECT with JOIN clause to calculate the
sum of values in a table based on a condition specified by the values in
another table.
This statement uses the SUM() function to calculate the total amounts of the
canceled orders:
SELECT
SUM(quantityOrdered * priceEach) cancelled_amount
FROM
orderdetails
INNER JOIN orders USING (orderNumber)
WHERE
status = 'Cancelled';
Code language: SQL (Structured Query Language) (sql)
If you want to rotate rows to columns, you can use the SUM() fnction
with CASE expression. It is kind of SUMIF logic:
SELECT
SUM(CASE
WHEN status = 'Shipped' THEN quantityOrdered
END) qty_shipped,
SUM(CASE
WHEN status = 'Resolved' THEN quantityOrdered
END) qty_resolved,
SUM(CASE
WHEN status = 'Cancelled' THEN quantityOrdered
END) qty_cancelled,
SUM(CASE
WHEN status = 'On Hold' THEN quantityOrdered
END) qty_on_hold,
SUM(CASE
WHEN status = 'Disputed' THEN quantityOrdered
END) qty_on_disputed,
SUM(CASE
WHEN status = 'In Process' THEN quantityOrdered
END) qty_in_process
FROM
orderdetails
INNER JOIN
orders USING (orderNumber);
Code language: SQL (Structured Query Language) (sql)