

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum columns corresponding values according to similar dates in MySQL?
For this, use aggregate function SUM() along with GROUP BY. Let us first create a table −
mysql> create table DemoTable1522 -> ( -> ProductPurchaseDate date, -> NumberOfProduct int -> ); Query OK, 0 rows affected (1.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1522 values('2019-01-21',45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1522 values('2018-12-31',78); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-01-21',67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-03-01',56); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1522 values('2018-01-21',97); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1522 values('2019-01-21',47); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1522;
This will produce the following output −
+---------------------+-----------------+ | ProductPurchaseDate | NumberOfProduct | +---------------------+-----------------+ | 2019-01-21 | 45 | | 2018-12-31 | 78 | | 2019-01-21 | 67 | | 2019-03-01 | 56 | | 2018-01-21 | 97 | | 2019-01-21 | 47 | +---------------------+-----------------+ 6 rows in set (0.00 sec)
Following is the query to count columns according to dates in MySQL −
mysql> select ProductPurchaseDate,sum(NumberOfProduct) from DemoTable1522 -> group by ProductPurchaseDate;
This will produce the following output −
+---------------------+----------------------+ | ProductPurchaseDate | sum(NumberOfProduct) | +---------------------+----------------------+ | 2019-01-21 | 159 | | 2018-12-31 | 78 | | 2019-03-01 | 56 | | 2018-01-21 | 97 | +---------------------+----------------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- Sum values in MySQL from similar day records
- MySQL query to calculate the days between two dates from different columns but similar rows
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- SUM corresponding duplicate records in MySQL
- Sum from array values with similar key in JavaScript
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- MySQL query to sum rows having repeated corresponding Id
- Sum similar numeric values within array of objects - JavaScript
- MySQL query to group by column and display the sum of similar values in another column
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- Return similar names from different columns with distinct space allocations in MySQL?
- How to multiply columns and then sum the rows with similar records like customer name?
- Sorting a HashMap according to values in Java
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- MySQL select only a single value from 5 similar values?
Advertisements