· 7 years ago · Oct 27, 2018, 09:06 PM
1CREATE TABLE IF NOT EXISTS `__example__` (
2`ID` int(11) NOT NULL AUTO_INCREMENT,
3`amount` decimal(10,2) DEFAULT NULL,
4`fromDate` datetime DEFAULT NULL,
5`toDate` datetime DEFAULT NULL,
6PRIMARY KEY (`ID`)
7) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
8
9INSERT INTO `__example__` (`ID`, `amount`, `fromDate`, `toDate`)
10VALUES (1, 1000.00, '2016-01-16 17:05:40', '2016-01-26 17:05:45');
11
12INSERT INTO `__example__` (`ID`, `amount`, `fromDate`, `toDate`)
13VALUES (2, 1000.00, '2016-01-26 17:05:40', '2016-02-05 17:05:45');
14
15INSERT INTO `__example__` (`ID`, `amount`, `fromDate`, `toDate`)
16VALUES (3, 1000.00, '2015-12-26 17:05:40', '2016-01-05 17:05:45');
17
18INSERT INTO `__example__` (`ID`, `amount`, `fromDate`, `toDate`)
19VALUES (4, 4000.00, '2015-12-26 17:05:40', '2016-02-04 17:05:45');
20
21year | month | sum
222015 | 12 | 1400.00
232016 | 01 | 4700.00
242016 | 02 | 900.00
25
26Select YEAR(fromDate) [Year], MONTH(fromDate) [Month], SUM(amount) [Amount]
27FROM '__example__'
28GROUP BY YEAR(fromDate), MONTH(fromDate)
29ORDER BY Year, Month