1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned NOT NULL auto_increment primary key,
`date` date NOT NULL,
`type` smallint unsigned NOT NULL,
`cout` integer unsigned NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`date`,`type`,`cout`) values
('2018-01-01', 1, 10),
('2018-01-01', 2, 5),
('2017-01-01', 1, 7),
('2018-01-01', 1, 2),
('2018-01-01', 3, 15),
('2017-01-01', 3, 20),
('2018-01-01', 2, 10),
('2017-01-01', 1, 5)
--------------
--------------
select * from `test`
--------------
+----+------------+------+------+
| id | date | type | cout |
+----+------------+------+------+
| 1 | 2018-01-01 | 1 | 10 |
| 2 | 2018-01-01 | 2 | 5 |
| 3 | 2017-01-01 | 1 | 7 |
| 4 | 2018-01-01 | 1 | 2 |
| 5 | 2018-01-01 | 3 | 15 |
| 6 | 2017-01-01 | 3 | 20 |
| 7 | 2018-01-01 | 2 | 10 |
| 8 | 2017-01-01 | 1 | 5 |
+----+------------+------+------+
--------------
select `type`,
year(`date`) as annee,
sum(cout) as cout
from `test`
group by `type`, year(`date`)
--------------
+------+-------+------+
| type | annee | cout |
+------+-------+------+
| 1 | 2018 | 12 |
| 2 | 2018 | 15 |
| 1 | 2017 | 12 |
| 3 | 2018 | 15 |
| 3 | 2017 | 20 |
+------+-------+------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager