Mysql sum select column

For example, you might wish to know how the combined total salary of all employees whose salary is above $50,000 / year.

SELECT SUM[salary] AS "Total Salary"
FROM employees
WHERE salary > 50000;

In this SUM function example, we've aliased the SUM[salary] expression as "Total Salary". As a result, "Total Salary" will display as the field name when the result set is returned.

Example - Using DISTINCT

You can use the DISTINCT clause within the SUM function. For example, the SQL statement below returns the combined total salary of unique salary values where the salary is above $50,000 / year.

SELECT SUM[DISTINCT salary] AS "Total Salary"
FROM employees
WHERE salary > 50000;

If there were two salaries of $82,000/year, only one of these values would be used in the SUM function.

Example - Using Formula

The expression contained within the SUM function does not need to be a single field. You could also use a formula. For example, you might want to calculate the total commission.

SELECT SUM[sales * 0.05] AS "Total Commission"
FROM orders;

Example - Using GROUP BY

In some cases, you will be required to use the GROUP BY clause with the SUM function.

For example, you could also use the SUM function to return the name of the department and the total sales [in the associated department].

SELECT department, SUM[sales] AS "Total sales"
FROM order_details
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the SUM function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

Our database has a table named game with data in the following columns: id, player, and score.

idplayerscore1John1342Tom1463Lucy204Tom1185Tom1026Lucy907Lucy348John122

Let’s find the total score obtained by all players.

Solution:

SELECT SUM[score] as sum_score
FROM game;

Here’s the result:

sum_score766

Discussion:

The aggregate function SUM is ideal for computing the sum of a column’s values. This function is used in a SELECT statement and takes the name of the column whose values you want to sum.

If you do not specify any other columns in the SELECT statement, then the sum will be calculated for all records in the table. In our example, we only select the sum and no other columns. Therefore, the query in our example returns the sum all scores [766].

Of course, we can also compute the total score earned by each player by using a GROUP BY clause and selecting each player’s name from the table, alongside the sum:

MySQL SUM[] function returns the sum of an expression. SUM[] function returns NULL when the return set has no rows.

Syntax:

SUM[[DISTINCT] expr]

Where expr is an expression.

The DISTINCT keyword can be used to sum only the distinct values of expr.

MySQL Version: 5.6

Contents:

Example: MySQL SUM[] function

The following MySQL statement returns the sum of 'total_cost' from purchase table.

Sample table: purchase


Code:

SELECT SUM[total_cost]             
FROM purchase;

Relational Algebra Expression:


Relational Algebra Tree:


Sample Output:

mysql> SELECT SUM[total_cost]             
    -> FROM purchase;
+-----------------+
| SUM[total_cost] |
+-----------------+
|         3590.00 | 
+-----------------+
1 row in set [0.00 sec]

PHP script:






example-SUM[]- php MySQL examples | w3resource




Sum of total costs of purchases:

Sum of total costs of purchases

View the example in browser

JSP script:








example-sum[]



Sum of total costs of purchases

Example: MySQL SUM[] function with where clause

MySQL SUM[] function with WHERE retrieves the sum of a given expression which is filtered against a condition placed after WHERE. The following MySQL statement returns the sum of 'total_cost' from purchase table for the category ['cate_id'] given with WHERE clause.

Sample table: purchase


Code:

SELECT SUM[total_cost] 
FROM purchase          
WHERE cate_id='CA001';

Relational Algebra Expression:


Relational Algebra Tree:


Sample Output:

mysql> SELECT SUM[total_cost] 
    -> FROM purchase          
    -> WHERE cate_id='CA001';
+-----------------+
| SUM[total_cost] |
+-----------------+
|         1725.00 | 
+-----------------+
1 row in set [0.00 sec]

PHP script:






example-aggregate-functions-and-grouping-sum-with-where- php MySQL examples | w3resource




Sum of total costs of purchases for category id CA001:

Sum of total costs of purchases for category id CA001

Bài mới nhất

Chủ Đề