
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to get the sum for every distinct value in another column in MySQL?
You can get the sum for every distinct value in another column with the help of aggregate function SUM() with GROUP BY command. To understand the above concept, let us create a table. The query to create a table is as follows:
mysql> create table SumOfEveryDistinct -> ( -> Id int not null, -> Amount int -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into SumOfEveryDistinct values(10,100); Query OK, 1 row affected (0.19 sec) mysql> insert into SumOfEveryDistinct values(11,200); Query OK, 1 row affected (0.20 sec) mysql> insert into SumOfEveryDistinct values(12,300); Query OK, 1 row affected (0.14 sec) mysql> insert into SumOfEveryDistinct values(10,400); Query OK, 1 row affected (0.20 sec) mysql> insert into SumOfEveryDistinct values(11,500); Query OK, 1 row affected (0.10 sec) mysql> insert into SumOfEveryDistinct values(12,600); Query OK, 1 row affected (0.13 sec) mysql> insert into SumOfEveryDistinct values(10,700); Query OK, 1 row affected (0.10 sec) mysql> insert into SumOfEveryDistinct values(11,800); Query OK, 1 row affected (0.18 sec) mysql> insert into SumOfEveryDistinct values(12,900); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from SumOfEveryDistinct;
The following is the output:
+----+--------+ | Id | Amount | +----+--------+ | 10 | 100 | | 11 | 200 | | 12 | 300 | | 10 | 400 | | 11 | 500 | | 12 | 600 | | 10 | 700 | | 11 | 800 | | 12 | 900 | +----+--------+ 9 rows in set (0.00 sec)
Here is the query to sum every distinct value in another column:
mysql> select Id, sum(Amount) as TotalSum from SumOfEveryDistinct -> group by Id;
The following is the output:
+----+----------+ | Id | TotalSum | +----+----------+ | 10 | 1200 | | 11 | 1500 | | 12 | 1800 | +----+----------+ 3 rows in set (0.00 sec)
- Related Articles
- How to get the count of each distinct value in a column in MySQL?
- How to count the distinct column in MySQL?
- How to select distinct value from one MySQL column only?
- How to get distinct values for non-key column fields in Laravel?
- MySQL query to get sum of each column where every column has same number of values?
- MySQL query to get the count of distinct records in a column
- How to find the sum of a column values up to a value in another column in R?
- Convert the case of every value in a MySQL Column to uppercase
- How to get the seed value of an identity column in MySQL?
- How to find the sum by distinct column for factor levels in an R data frame?
- Get first date from timestamp in MySQL group by another column with duplicate value
- How to sum cells in a column if a condition is met in another column with MySQL?
- MySQL query to group by column and display the sum of similar values in another column
- Get sum of column with conditions in MySQL
- Get the maximum count of distinct values in a separate column with MySQL

Advertisements