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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| --------------
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 `calendrier`
--------------
--------------
CREATE TABLE `calendrier`
( `annee` smallint unsigned NOT NULL,
`mois` tinyint unsigned NOT NULL,
primary key (`annee`,`mois`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `calendrier` (`annee`,`mois`) VALUES
(2018,1),(2018,2),(2018,3),(2018,4),(2018,5),(2018,6),(2018,7),(2018,8),(2018,9),(2018,10),(2018,11),(2018,12)
--------------
--------------
select * from `calendrier`
--------------
+-------+------+
| annee | mois |
+-------+------+
| 2018 | 1 |
| 2018 | 2 |
| 2018 | 3 |
| 2018 | 4 |
| 2018 | 5 |
| 2018 | 6 |
| 2018 | 7 |
| 2018 | 8 |
| 2018 | 9 |
| 2018 | 10 |
| 2018 | 11 |
| 2018 | 12 |
+-------+------+
--------------
DROP TABLE IF EXISTS `facture`
--------------
--------------
CREATE TABLE `facture`
( `id` integer unsigned NOT NULL auto_increment primary key,
`num` char(08) NOT NULL,
`projet` varchar(255) NOT NULL,
`montant` decimal(15,2) NOT NULL,
`annee` smallint unsigned generated always as (year(date_fact)) stored NOT NULL,
`mois` tinyint unsigned generated always as (month(date_fact)) stored NOT NULL,
`date_fact` date NOT NULL,
`date_encaiss` date NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `facture` (`num`, `projet`,`montant`,`date_fact`,`date_encaiss`) VALUES
('2018-001', 'projet 1', 100.00, '2018-02-05', '2018-03-01'),
('2018-002', 'projet 2', 50.00, '2018-04-17', '2018-04-25'),
('2018-003', 'projet 3', 75.00, '2018-04-25', '2018-05-03'),
('2018-004', 'projet 1', 30.00, '2018-06-02', '2018-06-25'),
('2018-005', 'projet 2', 45.00, '2018-06-08', '2018-07-05'),
('2018-006', 'projet 3', 12.00, '2018-06-12', '2018-06-25'),
('2018-007', 'projet 1', 55.00, '2018-06-25', '2018-07-05'),
('2018-008', 'projet 2', 64.00, '2018-09-11', '2018-09-25'),
('2018-009', 'projet 3', 200.00, '2018-09-25', '2018-10-03'),
('2018-010', 'projet 1', 150.00, '2018-09-11', '2018-09-25'),
('2018-011', 'projet 2', 88.00, '2018-10-15', NUll),
('2018-012', 'projet 3', 33.00, '2018-10-22', NULL)
--------------
--------------
select * from `facture`
--------------
+----+----------+----------+---------+-------+------+------------+--------------+
| id | num | projet | montant | annee | mois | date_fact | date_encaiss |
+----+----------+----------+---------+-------+------+------------+--------------+
| 1 | 2018-001 | projet 1 | 100.00 | 2018 | 2 | 2018-02-05 | 2018-03-01 |
| 2 | 2018-002 | projet 2 | 50.00 | 2018 | 4 | 2018-04-17 | 2018-04-25 |
| 3 | 2018-003 | projet 3 | 75.00 | 2018 | 4 | 2018-04-25 | 2018-05-03 |
| 4 | 2018-004 | projet 1 | 30.00 | 2018 | 6 | 2018-06-02 | 2018-06-25 |
| 5 | 2018-005 | projet 2 | 45.00 | 2018 | 6 | 2018-06-08 | 2018-07-05 |
| 6 | 2018-006 | projet 3 | 12.00 | 2018 | 6 | 2018-06-12 | 2018-06-25 |
| 7 | 2018-007 | projet 1 | 55.00 | 2018 | 6 | 2018-06-25 | 2018-07-05 |
| 8 | 2018-008 | projet 2 | 64.00 | 2018 | 9 | 2018-09-11 | 2018-09-25 |
| 9 | 2018-009 | projet 3 | 200.00 | 2018 | 9 | 2018-09-25 | 2018-10-03 |
| 10 | 2018-010 | projet 1 | 150.00 | 2018 | 9 | 2018-09-11 | 2018-09-25 |
| 11 | 2018-011 | projet 2 | 88.00 | 2018 | 10 | 2018-10-15 | NULL |
| 12 | 2018-012 | projet 3 | 33.00 | 2018 | 10 | 2018-10-22 | NULL |
+----+----------+----------+---------+-------+------+------------+--------------+
--------------
select annee,
sum(coalesce(montant, 0.00)) as facture,
sum( case when date_encaiss is null then 0.00 else montant end) as encaisse,
sum(coalesce(montant, 0.00) - case when date_encaiss is null then 0.00 else montant end) as diff
from `facture`
group by annee
--------------
+-------+---------+----------+--------+
| annee | facture | encaisse | diff |
+-------+---------+----------+--------+
| 2018 | 902.00 | 781.00 | 121.00 |
+-------+---------+----------+--------+
--------------
select t1.annee,
t1.mois,
sum(coalesce(montant, 0.00)) as facture,
sum( case when date_encaiss is null then 0.00 else montant end) as encaisse,
sum(coalesce(montant, 0.00) - case when date_encaiss is null then 0.00 else montant end) as diff
from `calendrier` as t1
left outer join `facture` as t2
on t2.annee = t1.annee
and t2.mois = t1.mois
group by t1.annee, t1.mois
order by t1.annee, t1.mois
--------------
+-------+------+---------+----------+--------+
| annee | mois | facture | encaisse | diff |
+-------+------+---------+----------+--------+
| 2018 | 1 | 0.00 | 0.00 | 0.00 |
| 2018 | 2 | 100.00 | 100.00 | 0.00 |
| 2018 | 3 | 0.00 | 0.00 | 0.00 |
| 2018 | 4 | 125.00 | 125.00 | 0.00 |
| 2018 | 5 | 0.00 | 0.00 | 0.00 |
| 2018 | 6 | 142.00 | 142.00 | 0.00 |
| 2018 | 7 | 0.00 | 0.00 | 0.00 |
| 2018 | 8 | 0.00 | 0.00 | 0.00 |
| 2018 | 9 | 414.00 | 414.00 | 0.00 |
| 2018 | 10 | 121.00 | 0.00 | 121.00 |
| 2018 | 11 | 0.00 | 0.00 | 0.00 |
| 2018 | 12 | 0.00 | 0.00 | 0.00 |
+-------+------+---------+----------+--------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager