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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `tab_client`
--------------
--------------
CREATE TABLE `tab_client`
( `cli_id` integer unsigned NOT NULL auto_increment primary key,
`cli_nom` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tab_client` (`cli_nom`) values
('albert'),
('bertrand'),
('charles'),
('didier'),
('eric')
--------------
--------------
select * from tab_client
--------------
+--------+----------+
| cli_id | cli_nom |
+--------+----------+
| 1 | albert |
| 2 | bertrand |
| 3 | charles |
| 4 | didier |
| 5 | eric |
+--------+----------+
--------------
DROP TABLE IF EXISTS `tab_produit`
--------------
--------------
CREATE TABLE `tab_produit`
( `prod_id` integer unsigned NOT NULL,
`prod_campagne` integer unsigned NOT NULL,
`prod_montant` decimal(15,2) NOT NULL,
primary key (`prod_id`,`prod_campagne`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tab_produit` (`prod_id`,`prod_campagne`,`prod_montant`) values
(1, 101, 12.00), (1, 102, 11.00), (1, 103, 10.00),
(2, 101, 33.00), (2, 102, 32.00), (2, 103, 31.00),
(3, 101, 17.00), (3, 102, 16.00), (3, 103, 15.00),
(4, 101, 45.00), (4, 102, 44.00), (4, 103, 43.00),
(5, 101, 5.00), (5, 102, 4.00), (5, 103, 3.00),
(6, 101, 9.00), (6, 102, 8.00), (6, 103, 7.00),
(7, 101, 21.00), (7, 102, 20.00), (7, 103, 19.00)
--------------
--------------
select * from tab_produit
--------------
+---------+---------------+--------------+
| prod_id | prod_campagne | prod_montant |
+---------+---------------+--------------+
| 1 | 101 | 12.00 |
| 1 | 102 | 11.00 |
| 1 | 103 | 10.00 |
| 2 | 101 | 33.00 |
| 2 | 102 | 32.00 |
| 2 | 103 | 31.00 |
| 3 | 101 | 17.00 |
| 3 | 102 | 16.00 |
| 3 | 103 | 15.00 |
| 4 | 101 | 45.00 |
| 4 | 102 | 44.00 |
| 4 | 103 | 43.00 |
| 5 | 101 | 5.00 |
| 5 | 102 | 4.00 |
| 5 | 103 | 3.00 |
| 6 | 101 | 9.00 |
| 6 | 102 | 8.00 |
| 6 | 103 | 7.00 |
| 7 | 101 | 21.00 |
| 7 | 102 | 20.00 |
| 7 | 103 | 19.00 |
+---------+---------------+--------------+
--------------
DROP TABLE IF EXISTS `tab_paiement`
--------------
--------------
CREATE TABLE `tab_paiement`
( `pay_id` integer unsigned NOT NULL auto_increment primary key,
`pay_campagne` integer unsigned NOT NULL,
`pay_client` integer unsigned NOT NULL,
`pay_montant` decimal(15,2) NOT NULL,
CONSTRAINT `FK_PAIEMENT_CLIENT` FOREIGN KEY (`pay_client`) REFERENCES `tab_client` (`cli_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tab_paiement` (`pay_campagne`,`pay_client`,`pay_montant`) values
(101, 1, 12.00), (102, 1, 16.00), (103, 1, 3.00),
(101, 2, 12.00), (102, 2, 16.00), (103, 2, 7.00),
(101, 3, 33.00), (102, 3, 44.00), (103, 3, 14.00),
(101, 4, 33.00), (102, 4, 44.00), (103, 4, 19.00),
(101, 5, 17.00), (102, 5, 4.00), (103, 5, 19.00)
--------------
--------------
select * from tab_paiement
--------------
+--------+--------------+------------+-------------+
| pay_id | pay_campagne | pay_client | pay_montant |
+--------+--------------+------------+-------------+
| 1 | 101 | 1 | 12.00 |
| 2 | 102 | 1 | 16.00 |
| 3 | 103 | 1 | 3.00 |
| 4 | 101 | 2 | 12.00 |
| 5 | 102 | 2 | 16.00 |
| 6 | 103 | 2 | 7.00 |
| 7 | 101 | 3 | 33.00 |
| 8 | 102 | 3 | 44.00 |
| 9 | 103 | 3 | 14.00 |
| 10 | 101 | 4 | 33.00 |
| 11 | 102 | 4 | 44.00 |
| 12 | 103 | 4 | 19.00 |
| 13 | 101 | 5 | 17.00 |
| 14 | 102 | 5 | 4.00 |
| 15 | 103 | 5 | 19.00 |
+--------+--------------+------------+-------------+
--------------
DROP TABLE IF EXISTS `tab_facture`
--------------
--------------
CREATE TABLE `tab_facture`
( `fac_id` integer unsigned NOT NULL auto_increment primary key,
`fac_prod` integer unsigned NOT NULL,
`fac_client` integer unsigned NOT NULL,
`fac_campagne` integer unsigned NOT NULL,
CONSTRAINT `FK_FACTURE_PRODUIT` FOREIGN KEY (`fac_prod`,`fac_campagne`) REFERENCES `tab_produit` (`prod_id`,`prod_campagne`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_FACTURE_CLIENT` FOREIGN KEY (`fac_client`) REFERENCES `tab_client` (`cli_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tab_facture` (`fac_prod`,`fac_client`,`fac_campagne`) values
(1, 1, 101), (1, 2, 101),
(2, 3, 101), (2, 4, 101),
(3, 5, 101), (3, 1, 102), (3, 2, 102),
(4, 3, 102), (4, 4, 102),
(5, 5, 102), (5, 1, 103),
(6, 2, 103), (6, 3, 103),
(7, 4, 103), (7, 5, 103)
--------------
--------------
select * from tab_facture
--------------
+--------+----------+------------+--------------+
| fac_id | fac_prod | fac_client | fac_campagne |
+--------+----------+------------+--------------+
| 1 | 1 | 1 | 101 |
| 2 | 1 | 2 | 101 |
| 3 | 2 | 3 | 101 |
| 4 | 2 | 4 | 101 |
| 5 | 3 | 5 | 101 |
| 6 | 3 | 1 | 102 |
| 7 | 3 | 2 | 102 |
| 8 | 4 | 3 | 102 |
| 9 | 4 | 4 | 102 |
| 10 | 5 | 5 | 102 |
| 11 | 5 | 1 | 103 |
| 12 | 6 | 2 | 103 |
| 13 | 6 | 3 | 103 |
| 14 | 7 | 4 | 103 |
| 15 | 7 | 5 | 103 |
+--------+----------+------------+--------------+
--------------
select t1.nom_client, total_produit, total_paiement,
case abs(total_produit - total_paiement) when 0.00 then 'ok' else 'différence' end as controle
from (
select cli_id, cli_nom as nom_client, sum(prod_montant) as total_produit
from tab_produit
inner join tab_facture
on fac_campagne = prod_campagne
and fac_prod = prod_id
inner join tab_client
on cli_id = fac_client
group by cli_id, cli_nom
) as t1
left outer join (
select cli_id, cli_nom as nom_client, sum(pay_montant) as total_paiement
from tab_paiement
inner join tab_facture
on fac_campagne = pay_campagne
and fac_client = pay_client
inner join tab_client
on cli_id = pay_client
group by cli_id, cli_nom
) as t2
on t2.cli_id = t1.cli_id
--------------
+------------+---------------+----------------+------------+
| nom_client | total_produit | total_paiement | controle |
+------------+---------------+----------------+------------+
| albert | 31.00 | 31.00 | ok |
| bertrand | 35.00 | 35.00 | ok |
| charles | 84.00 | 91.00 | différence |
| didier | 96.00 | 96.00 | ok |
| eric | 40.00 | 40.00 | ok |
+------------+---------------+----------------+------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager