
- 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 selected column values based on specific month records in MySQL?
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, PurchaseDate date, SalePrice int ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(PurchaseDate,SalePrice) values('2018-01-10',450); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(PurchaseDate,SalePrice) values('2019-12-25',1000); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(PurchaseDate,SalePrice) values('2016-12-02',5560); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(PurchaseDate,SalePrice) values('2015-02-20',4550); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(PurchaseDate,SalePrice) values('2015-12-11',4110); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------------+-----------+ | Id | PurchaseDate | SalePrice | +----+--------------+-----------+ | 1 | 2018-01-10 | 450 | | 2 | 2019-12-25 | 1000 | | 3 | 2016-12-02 | 5560 | | 4 | 2015-02-20 | 4550 | | 5 | 2015-12-11 | 4110 | +----+--------------+-----------+ 5 rows in set (0.00 sec)
Following is the query to sum selected column values in MySQL based on a specific month. For example, here only the SalePrice has added for PurchaseDate in 12th month i.e. December −
mysql> select SUM(SalePrice) from DemoTable where month(PurchaseDate)=12;
The above adds records (SalePrice) for only the selected month i.e. 12th month December −
2019-12-25 2016-12-02 2015-12-11
This will produce the following output −
+----------------+ | SUM(SalePrice) | | 10670 | +----------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL to fetch records based on a specific month and year?
- MySQL query for grouping and summing the values based on specific records
- How to sum current month records in MySQL?
- Selecting data from a MySQL table based on a specific month?
- SUM a column based on a condition in MySQL
- Find specific records from a column with comma separated values in MySQL
- MySQL SELECT query to return records with specific month and year
- How can fetch records from specific month and year in a MySQL table?
- Find the sum of a column values based on another numerical column in R.
- Replace records based on conditions in MySQL?
- Sum up values in a single MySQL column in a specific way?
- How to sum based on field value in MySQL?
- How to sum the values in the table by month with MySQL?
- How to Auto-Number a Column Based on the Cell Values on Another Column in Excel?
- How to change row values based on column values in an R data frame?

Advertisements