
- 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 sum the values in the table by month with MySQL?
For this, use EXTRACT(), that would allow you to extract specific month records. For example, to add all the prices in January (irrespective of the year).
Let us first create a −
mysql> create table DemoTable1415 -> ( -> ProductPurchaseDate date, -> ProductPrice int -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1415 values('2019-01-12',560); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1415 values('2018-01-14',1060); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1415 values('2017-03-21',780); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1415 values('2016-09-01',800); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1415 values('2019-01-14',100); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select −
mysql> select * from DemoTable1415;
This will produce the following output −
+---------------------+--------------+ | ProductPurchaseDate | ProductPrice | +---------------------+--------------+ | 2019-01-12 | 560 | | 2018-01-14 | 1060 | | 2017-03-21 | 780 | | 2016-09-01 | 800 | | 2019-01-14 | 100 | +---------------------+--------------+ 5 rows in set (0.00 sec)
Following is the query to sum the values in the table by month −
mysql> select extract(MONTH from ProductPurchaseDate) as month,sum(ProductPrice) as total_value from DemoTable1415 -> group by month;
This will produce the following output −
+-------+-------------+ | month | total_value | +-------+-------------+ | 1 | 1720 | | 3 | 780 | | 9 | 800 | +-------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How to sum selected column values based on specific month records in MySQL?
- How to sum current month records in MySQL?
- Reshuffle the values in a table with MySQL
- How to MySQL SELECT by month?
- How to select the sum of the column values with higher value in reach row with MySQL?
- How to sum the score of students with the same name in MySQL with ORDER BY?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How to update a MySQL table by swapping two column values?
- Insert all the values in a table with a single MySQL query separating records by comma
- How can we find the duplicate values available in a MySQL table by using JOINS?
- How can we subtract values in MySQL table with the help of LEFT JOIN?
- Get the sum of multiple row (not all) values from a MySQL table?
- Updating a MySQL table with values from another table?
- How to divide the row values by row sum in R matrix?

Advertisements