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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
| --------------
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 `produit`
--------------
--------------
create table `produit`
( `id` integer unsigned not null auto_increment primary key,
`code` varchar(255) not null,
`nom` text not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `produit` (`code`,`nom`) VALUES
('code 1', 'produit 1'),
('code 2', 'produit 2'),
('code 3', 'produit 3'),
('code 4', 'produit 4'),
('code 5', 'produit 5'),
('code 6', 'produit 6')
--------------
--------------
select * from `produit`
--------------
+----+--------+-----------+
| id | code | nom |
+----+--------+-----------+
| 1 | code 1 | produit 1 |
| 2 | code 2 | produit 2 |
| 3 | code 3 | produit 3 |
| 4 | code 4 | produit 4 |
| 5 | code 5 | produit 5 |
| 6 | code 6 | produit 6 |
+----+--------+-----------+
--------------
DROP TABLE IF EXISTS `mouvement`
--------------
--------------
create table `mouvement`
( `id` integer unsigned not null auto_increment primary key,
`date` date not null,
`sens` char(01) not null,
`type` char(01) not null,
`produit` integer unsigned not null,
`quantite` integer unsigned not null,
`depot` char(03) not null,
CONSTRAINT `FK_01` FOREIGN KEY (`produit`) REFERENCES `produit` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `mouvement` (`date`,`sens`,`type`,`produit`,`quantite`,`depot`) VALUES
('2019-05-01','E','L',1,25,'AU1'),('2019-05-02','S','U',1,02,'AU1'),('2019-05-03','S','D',1,02,'AU1'),
('2019-05-04','E','L',1,25,'AU1'),('2019-05-05','S','U',1,02,'AU1'),('2019-05-06','S','D',1,02,'AU1'),
('2019-05-01','E','L',2,44,'LO2'),('2019-05-02','S','U',2,03,'LO2'),('2019-05-03','S','D',2,03,'LO2'),
('2019-05-04','E','L',2,44,'LO2'),('2019-05-05','S','U',2,03,'LO2'),('2019-05-06','S','D',2,03,'LO2'),
('2019-05-04','E','L',3,75,'SP3'),('2019-05-05','S','U',3,02,'SP3'),('2019-05-06','S','D',3,02,'SP3'),
('2019-05-07','E','L',3,75,'SP3'),('2019-05-08','S','U',3,02,'SP3'),('2019-05-09','S','D',3,02,'SP3'),
('2019-05-04','E','L',4,26,'SP4'),('2019-05-04','S','U',4,01,'SP4'),('2019-05-05','S','D',4,01,'SP4'),
('2019-05-05','E','L',4,26,'SP4'),('2019-05-06','S','U',4,01,'SP4'),('2019-05-06','S','D',4,01,'SP4'),
('2019-05-05','E','L',5,26,'MR5'),('2019-05-05','S','U',5,01,'MR5'),('2019-05-05','S','D',5,01,'MR5'),
('2019-05-07','E','L',5,26,'MR5'),('2019-05-07','S','U',5,01,'MR5'),('2019-05-07','S','D',5,01,'MR5'),
('2019-05-08','E','L',6,26,'AN6'),('2019-05-08','S','U',6,01,'AN6'),('2019-05-08','S','D',6,01,'AN6'),
('2019-05-08','E','L',6,26,'AN6'),('2019-05-08','S','U',6,01,'AN6'),('2019-05-08','S','D',6,01,'AN6')
--------------
--------------
select * from `mouvement`
--------------
+----+------------+------+------+---------+----------+-------+
| id | date | sens | type | produit | quantite | depot |
+----+------------+------+------+---------+----------+-------+
| 1 | 2019-05-01 | E | L | 1 | 25 | AU1 |
| 2 | 2019-05-02 | S | U | 1 | 2 | AU1 |
| 3 | 2019-05-03 | S | D | 1 | 2 | AU1 |
| 4 | 2019-05-04 | E | L | 1 | 25 | AU1 |
| 5 | 2019-05-05 | S | U | 1 | 2 | AU1 |
| 6 | 2019-05-06 | S | D | 1 | 2 | AU1 |
| 7 | 2019-05-01 | E | L | 2 | 44 | LO2 |
| 8 | 2019-05-02 | S | U | 2 | 3 | LO2 |
| 9 | 2019-05-03 | S | D | 2 | 3 | LO2 |
| 10 | 2019-05-04 | E | L | 2 | 44 | LO2 |
| 11 | 2019-05-05 | S | U | 2 | 3 | LO2 |
| 12 | 2019-05-06 | S | D | 2 | 3 | LO2 |
| 13 | 2019-05-04 | E | L | 3 | 75 | SP3 |
| 14 | 2019-05-05 | S | U | 3 | 2 | SP3 |
| 15 | 2019-05-06 | S | D | 3 | 2 | SP3 |
| 16 | 2019-05-07 | E | L | 3 | 75 | SP3 |
| 17 | 2019-05-08 | S | U | 3 | 2 | SP3 |
| 18 | 2019-05-09 | S | D | 3 | 2 | SP3 |
| 19 | 2019-05-04 | E | L | 4 | 26 | SP4 |
| 20 | 2019-05-04 | S | U | 4 | 1 | SP4 |
| 21 | 2019-05-05 | S | D | 4 | 1 | SP4 |
| 22 | 2019-05-05 | E | L | 4 | 26 | SP4 |
| 23 | 2019-05-06 | S | U | 4 | 1 | SP4 |
| 24 | 2019-05-06 | S | D | 4 | 1 | SP4 |
| 25 | 2019-05-05 | E | L | 5 | 26 | MR5 |
| 26 | 2019-05-05 | S | U | 5 | 1 | MR5 |
| 27 | 2019-05-05 | S | D | 5 | 1 | MR5 |
| 28 | 2019-05-07 | E | L | 5 | 26 | MR5 |
| 29 | 2019-05-07 | S | U | 5 | 1 | MR5 |
| 30 | 2019-05-07 | S | D | 5 | 1 | MR5 |
| 31 | 2019-05-08 | E | L | 6 | 26 | AN6 |
| 32 | 2019-05-08 | S | U | 6 | 1 | AN6 |
| 33 | 2019-05-08 | S | D | 6 | 1 | AN6 |
| 34 | 2019-05-08 | E | L | 6 | 26 | AN6 |
| 35 | 2019-05-08 | S | U | 6 | 1 | AN6 |
| 36 | 2019-05-08 | S | D | 6 | 1 | AN6 |
+----+------------+------+------+---------+----------+-------+
--------------
select t2.id,
t2.code,
t2.nom,
t1.id,
t1.date,
t1.sens,
t1.type,
t1.produit,
t1.quantite,
t1.depot
from `mouvement` as t1
inner join `produit` as t2
on t2.id = t1.produit
order by t2.id
--------------
+----+--------+-----------+----+------------+------+------+---------+----------+-------+
| id | code | nom | id | date | sens | type | produit | quantite | depot |
+----+--------+-----------+----+------------+------+------+---------+----------+-------+
| 1 | code 1 | produit 1 | 1 | 2019-05-01 | E | L | 1 | 25 | AU1 |
| 1 | code 1 | produit 1 | 2 | 2019-05-02 | S | U | 1 | 2 | AU1 |
| 1 | code 1 | produit 1 | 3 | 2019-05-03 | S | D | 1 | 2 | AU1 |
| 1 | code 1 | produit 1 | 4 | 2019-05-04 | E | L | 1 | 25 | AU1 |
| 1 | code 1 | produit 1 | 5 | 2019-05-05 | S | U | 1 | 2 | AU1 |
| 1 | code 1 | produit 1 | 6 | 2019-05-06 | S | D | 1 | 2 | AU1 |
| 2 | code 2 | produit 2 | 7 | 2019-05-01 | E | L | 2 | 44 | LO2 |
| 2 | code 2 | produit 2 | 8 | 2019-05-02 | S | U | 2 | 3 | LO2 |
| 2 | code 2 | produit 2 | 9 | 2019-05-03 | S | D | 2 | 3 | LO2 |
| 2 | code 2 | produit 2 | 10 | 2019-05-04 | E | L | 2 | 44 | LO2 |
| 2 | code 2 | produit 2 | 11 | 2019-05-05 | S | U | 2 | 3 | LO2 |
| 2 | code 2 | produit 2 | 12 | 2019-05-06 | S | D | 2 | 3 | LO2 |
| 3 | code 3 | produit 3 | 13 | 2019-05-04 | E | L | 3 | 75 | SP3 |
| 3 | code 3 | produit 3 | 14 | 2019-05-05 | S | U | 3 | 2 | SP3 |
| 3 | code 3 | produit 3 | 15 | 2019-05-06 | S | D | 3 | 2 | SP3 |
| 3 | code 3 | produit 3 | 16 | 2019-05-07 | E | L | 3 | 75 | SP3 |
| 3 | code 3 | produit 3 | 17 | 2019-05-08 | S | U | 3 | 2 | SP3 |
| 3 | code 3 | produit 3 | 18 | 2019-05-09 | S | D | 3 | 2 | SP3 |
| 4 | code 4 | produit 4 | 19 | 2019-05-04 | E | L | 4 | 26 | SP4 |
| 4 | code 4 | produit 4 | 20 | 2019-05-04 | S | U | 4 | 1 | SP4 |
| 4 | code 4 | produit 4 | 21 | 2019-05-05 | S | D | 4 | 1 | SP4 |
| 4 | code 4 | produit 4 | 22 | 2019-05-05 | E | L | 4 | 26 | SP4 |
| 4 | code 4 | produit 4 | 23 | 2019-05-06 | S | U | 4 | 1 | SP4 |
| 4 | code 4 | produit 4 | 24 | 2019-05-06 | S | D | 4 | 1 | SP4 |
| 5 | code 5 | produit 5 | 25 | 2019-05-05 | E | L | 5 | 26 | MR5 |
| 5 | code 5 | produit 5 | 26 | 2019-05-05 | S | U | 5 | 1 | MR5 |
| 5 | code 5 | produit 5 | 27 | 2019-05-05 | S | D | 5 | 1 | MR5 |
| 5 | code 5 | produit 5 | 28 | 2019-05-07 | E | L | 5 | 26 | MR5 |
| 5 | code 5 | produit 5 | 29 | 2019-05-07 | S | U | 5 | 1 | MR5 |
| 5 | code 5 | produit 5 | 30 | 2019-05-07 | S | D | 5 | 1 | MR5 |
| 6 | code 6 | produit 6 | 31 | 2019-05-08 | E | L | 6 | 26 | AN6 |
| 6 | code 6 | produit 6 | 32 | 2019-05-08 | S | U | 6 | 1 | AN6 |
| 6 | code 6 | produit 6 | 33 | 2019-05-08 | S | D | 6 | 1 | AN6 |
| 6 | code 6 | produit 6 | 34 | 2019-05-08 | E | L | 6 | 26 | AN6 |
| 6 | code 6 | produit 6 | 35 | 2019-05-08 | S | U | 6 | 1 | AN6 |
| 6 | code 6 | produit 6 | 36 | 2019-05-08 | S | D | 6 | 1 | AN6 |
+----+--------+-----------+----+------------+------+------+---------+----------+-------+
--------------
drop view if exists `vue_1`
--------------
--------------
create view `vue_1` as
select t2.nom,
t1.depot,
t1.type,
sum(case t1.sens when 'E' then +t1.quantite
when 'S' then -t1.quantite
else 0 end) as quantite
from `mouvement` as t1
inner join `produit` as t2
on t2.id = t1.produit
group by t2.nom, t1.depot, t1.type
--------------
--------------
select *
from `vue_1`
--------------
+-----------+-------+------+----------+
| nom | depot | type | quantite |
+-----------+-------+------+----------+
| produit 1 | AU1 | L | 50 |
| produit 1 | AU1 | U | -4 |
| produit 1 | AU1 | D | -4 |
| produit 2 | LO2 | L | 88 |
| produit 2 | LO2 | U | -6 |
| produit 2 | LO2 | D | -6 |
| produit 3 | SP3 | L | 150 |
| produit 3 | SP3 | U | -4 |
| produit 3 | SP3 | D | -4 |
| produit 4 | SP4 | L | 52 |
| produit 4 | SP4 | U | -2 |
| produit 4 | SP4 | D | -2 |
| produit 5 | MR5 | L | 52 |
| produit 5 | MR5 | U | -2 |
| produit 5 | MR5 | D | -2 |
| produit 6 | AN6 | L | 52 |
| produit 6 | AN6 | U | -2 |
| produit 6 | AN6 | D | -2 |
+-----------+-------+------+----------+
--------------
drop view if exists `vue_2`
--------------
--------------
create view `vue_2` as
select nom, case when depot = 'AU1' and type = 'D' then sum(quantite) else 0 end as DAU1,
case when depot = 'AU1' and type = 'L' then sum(quantite) else 0 end as LAU1,
case when depot = 'AU1' and type = 'U' then sum(quantite) else 0 end as UAU1,
case when depot = 'LO2' and type = 'D' then sum(quantite) else 0 end as DLO2,
case when depot = 'LO2' and type = 'L' then sum(quantite) else 0 end as LLO2,
case when depot = 'LO2' and type = 'U' then sum(quantite) else 0 end as ULO2,
case when depot = 'SP3' and type = 'D' then sum(quantite) else 0 end as DSP3,
case when depot = 'SP3' and type = 'L' then sum(quantite) else 0 end as LSP3,
case when depot = 'SP3' and type = 'U' then sum(quantite) else 0 end as USP3,
case when depot = 'SP4' and type = 'D' then sum(quantite) else 0 end as DSP4,
case when depot = 'SP4' and type = 'L' then sum(quantite) else 0 end as LSP4,
case when depot = 'SP4' and type = 'U' then sum(quantite) else 0 end as USP4,
case when depot = 'MR5' and type = 'D' then sum(quantite) else 0 end as DMR5,
case when depot = 'MR5' and type = 'L' then sum(quantite) else 0 end as LMR5,
case when depot = 'MR5' and type = 'U' then sum(quantite) else 0 end as UMR5,
case when depot = 'AN6' and type = 'D' then sum(quantite) else 0 end as DAN6,
case when depot = 'AN6' and type = 'L' then sum(quantite) else 0 end as LAN6,
case when depot = 'AN6' and type = 'U' then sum(quantite) else 0 end as UAN6
from `vue_1`
group by nom, depot, type
--------------
--------------
select * from `vue_2`
--------------
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| nom | DAU1 | LAU1 | UAU1 | DLO2 | LLO2 | ULO2 | DSP3 | LSP3 | USP3 | DSP4 | LSP4 | USP4 | DMR5 | LMR5 | UMR5 | DAN6 | LAN6 | UAN6 |
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| produit 1 | 0 | 50 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| produit 1 | 0 | 0 | -4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |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 0 | 0 | 0 | 0 | 0 | 0 | -2 | 0 | 0 | 0 | 0 | 0 |
| produit 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 52 | 0 |
| produit 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -2 |
| produit 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -2 | 0 | 0 |
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
--------------
drop view if exists `vue_3`
--------------
--------------
create view `vue_3` as
select nom, sum(DAU1) as DAU1, sum(LAU1) as LAU1, sum(UAU1) as UAU1,
sum(DLO2) as DLO2, sum(LLO2) as LLO2, sum(ULO2) as ULO2,
sum(DSP3) as DSP3, sum(LSP3) as LSP3, sum(USP3) as USP3,
sum(DSP4) as DSP4, sum(LSP4) as LSP4, sum(USP4) as USP4,
sum(DMR5) as DMR5, sum(LMR5) as LMR5, sum(UMR5) as UMR5,
sum(DAN6) as DAN6, sum(LAN6) as LAN6, sum(UAN6) as UAN6
from `vue_2`
group by nom
--------------
--------------
select * from `vue_3`
--------------
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| nom | DAU1 | LAU1 | UAU1 | DLO2 | LLO2 | ULO2 | DSP3 | LSP3 | USP3 | DSP4 | LSP4 | USP4 | DMR5 | LMR5 | UMR5 | DAN6 | LAN6 | UAN6 |
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| produit 1 | -4 | 50 | -4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| produit 2 | 0 | 0 | 0 | -6 | 88 | -6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| produit 3 | 0 | 0 | 0 | 0 | 0 | 0 | -4 | 150 | -4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| produit 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -2 | 52 | -2 | 0 | 0 | 0 | 0 | 0 | 0 |
| produit 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -2 | 52 | -2 | 0 | 0 | 0 |
| produit 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | -2 | 52 | -2 |
+-----------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager