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
| --------------
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 `distributeur`
--------------
--------------
CREATE TABLE `distributeur`
( `id` integer unsigned NOT NULL auto_increment primary key,
`nom` varchar(255) NOT NULL,
`categorie` integer unsigned NOT NULL,
`localisation` integer unsigned NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `distributeur` (`nom`,`categorie`,`localisation`) VALUES
('Sodifram Kaweni', 0, 1),
('Jumbo Score Hauts Vallons', 0, 1),
('Shopi Mamoudzou', 0, 1),
('Hyper Discount Kaweni', 0, 1),
('Sodicash Bandrélé', 2, 1)
--------------
--------------
select * from `distributeur`
--------------
+----+---------------------------+-----------+--------------+
| id | nom | categorie | localisation |
+----+---------------------------+-----------+--------------+
| 1 | Sodifram Kaweni | 0 | 1 |
| 2 | Jumbo Score Hauts Vallons | 0 | 1 |
| 3 | Shopi Mamoudzou | 0 | 1 |
| 4 | Hyper Discount Kaweni | 0 | 1 |
| 5 | Sodicash Bandrélé | 2 | 1 |
+----+---------------------------+-----------+--------------+
--------------
DROP TABLE IF EXISTS `produit`
--------------
--------------
CREATE TABLE `produit`
( `id` integer unsigned NOT NULL auto_increment primary key,
`designation` varchar(255) NOT NULL,
`poids` decimal(5,3) NOT NULL,
`litrage` decimal(5,3) NOT NULL,
`id_marque` integer unsigned NOT NULL,
`id_categorie` integer unsigned NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `produit` (`designation`,`poids`,`litrage`,`id_marque`,`id_categorie`) VALUES
('Riz blanc', 5.000, 0.000, 1, 7),
('Yahourts Vanille', 0.000, 0.100, 2, 2),
('Frites Surgelées SteakHouse', 1.100, 0.000, 4, 3)
--------------
--------------
select * from `produit`
--------------
+----+-----------------------------+-------+---------+-----------+--------------+
| id | designation | poids | litrage | id_marque | id_categorie |
+----+-----------------------------+-------+---------+-----------+--------------+
| 1 | Riz blanc | 5.000 | 0.000 | 1 | 7 |
| 2 | Yahourts Vanille | 0.000 | 0.100 | 2 | 2 |
| 3 | Frites Surgelées SteakHouse | 1.100 | 0.000 | 4 | 3 |
+----+-----------------------------+-------+---------+-----------+--------------+
--------------
DROP TABLE IF EXISTS `prix`
--------------
--------------
CREATE TABLE `prix`
( `id` integer unsigned NOT NULL auto_increment primary key,
`id_produit` integer unsigned NOT NULL,
`id_distributeur` integer unsigned NOT NULL,
`localisation` integer unsigned NOT NULL,
`prix` decimal(5,2) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `prix` (`id_produit`,`id_distributeur`,`localisation`,`prix`,`date`) VALUES
(1, 2, 1, 10.00, '2018-12-02'),
(2, 1, 1, 2.00, '2018-12-01'),
(3, 5, 1, 3.95, '2018-11-23'),
(1, 3, 1, 5.00, '2018-12-04'),
(1, 3, 1, 6.80, '2018-12-04'),
(2, 4, 1, 20.00, '2018-12-04'),
(3, 1, 1, 17.00, '2018-12-04')
--------------
--------------
select * from `prix`
--------------
+----+------------+-----------------+--------------+-------+------------+
| id | id_produit | id_distributeur | localisation | prix | date |
+----+------------+-----------------+--------------+-------+------------+
| 1 | 1 | 2 | 1 | 10.00 | 2018-12-02 |
| 2 | 2 | 1 | 1 | 2.00 | 2018-12-01 |
| 3 | 3 | 5 | 1 | 3.95 | 2018-11-23 |
| 4 | 1 | 3 | 1 | 5.00 | 2018-12-04 |
| 5 | 1 | 3 | 1 | 6.80 | 2018-12-04 |
| 6 | 2 | 4 | 1 | 20.00 | 2018-12-04 |
| 7 | 3 | 1 | 1 | 17.00 | 2018-12-04 |
+----+------------+-----------------+--------------+-------+------------+
--------------
select *
from `prix` as t1
left outer join `distributeur` as t2
on t2.id = t1.id_distributeur
left outer join `produit` as t3
on t3.id = t1.id_produit
--------------
+----+------------+-----------------+--------------+-------+------------+------+---------------------------+-----------+--------------+------+-----------------------------+-------+---------+-----------+--------------+
| id | id_produit | id_distributeur | localisation | prix | date | id | nom | categorie | localisation | id | designation | poids | litrage | id_marque | id_categorie |
+----+------------+-----------------+--------------+-------+------------+------+---------------------------+-----------+--------------+------+-----------------------------+-------+---------+-----------+--------------+
| 1 | 1 | 2 | 1 | 10.00 | 2018-12-02 | 2 | Jumbo Score Hauts Vallons | 0 | 1 | 1 | Riz blanc | 5.000 | 0.000 | 1 | 7 |
| 4 | 1 | 3 | 1 | 5.00 | 2018-12-04 | 3 | Shopi Mamoudzou | 0 | 1 | 1 | Riz blanc | 5.000 | 0.000 | 1 | 7 |
| 5 | 1 | 3 | 1 | 6.80 | 2018-12-04 | 3 | Shopi Mamoudzou | 0 | 1 | 1 | Riz blanc | 5.000 | 0.000 | 1 | 7 |
| 2 | 2 | 1 | 1 | 2.00 | 2018-12-01 | 1 | Sodifram Kaweni | 0 | 1 | 2 | Yahourts Vanille | 0.000 | 0.100 | 2 | 2 |
| 6 | 2 | 4 | 1 | 20.00 | 2018-12-04 | 4 | Hyper Discount Kaweni | 0 | 1 | 2 | Yahourts Vanille | 0.000 | 0.100 | 2 | 2 |
| 3 | 3 | 5 | 1 | 3.95 | 2018-11-23 | 5 | Sodicash Bandrélé | 2 | 1 | 3 | Frites Surgelées SteakHouse | 1.100 | 0.000 | 4 | 3 |
| 7 | 3 | 1 | 1 | 17.00 | 2018-12-04 | 1 | Sodifram Kaweni | 0 | 1 | 3 | Frites Surgelées SteakHouse | 1.100 | 0.000 | 4 | 3 |
+----+------------+-----------------+--------------+-------+------------+------+---------------------------+-----------+--------------+------+-----------------------------+-------+---------+-----------+--------------+
--------------
drop view if exists `vue`
--------------
--------------
create view `vue` as
select t1.id,
t3.designation,
t3.poids,
t3.litrage
from `prix` as t1
left outer join `distributeur` as t2
on t2.id = t1.id_distributeur
left outer join `produit` as t3
on t3.id = t1.id_produit
--------------
--------------
select * from `vue`
--------------
+----+-----------------------------+-------+---------+
| id | designation | poids | litrage |
+----+-----------------------------+-------+---------+
| 1 | Riz blanc | 5.000 | 0.000 |
| 4 | Riz blanc | 5.000 | 0.000 |
| 5 | Riz blanc | 5.000 | 0.000 |
| 2 | Yahourts Vanille | 0.000 | 0.100 |
| 6 | Yahourts Vanille | 0.000 | 0.100 |
| 3 | Frites Surgelées SteakHouse | 1.100 | 0.000 |
| 7 | Frites Surgelées SteakHouse | 1.100 | 0.000 |
+----+-----------------------------+-------+---------+
--------------
commit
--------------
--------------
select designation,
max(poids_1) as poids_1,
max(poids_2) as poids_2,
max(poids_3) as poids_3,
max(litrage_1) as litrage_1,
max(litrage_2) as litrage_2,
max(litrage_3) as litrage_3
from ( select designation,
case when rang = 1 then poids else null end as poids_1,
case when rang = 2 then poids else null end as poids_2,
case when rang = 3 then poids else null end as poids_3,
case when rang = 1 then litrage else null end as litrage_1,
case when rang = 2 then litrage else null end as litrage_2,
case when rang = 3 then litrage else null end as litrage_3
from ( select t1.designation,
count(t2.id) as rang,
any_value(t1.poids) as poids,
any_value(t1.litrage) as litrage
from `vue` as t1
inner join `vue` as t2
on t2.designation = t1.designation
and t2.id <= t1.id
group by t1.designation, t1.id
order by t1.designation, t1.id
) as y
group by designation, rang
) as x
group by designation
--------------
+-----------------------------+---------+---------+---------+-----------+-----------+-----------+
| designation | poids_1 | poids_2 | poids_3 | litrage_1 | litrage_2 | litrage_3 |
+-----------------------------+---------+---------+---------+-----------+-----------+-----------+
| Frites Surgelées SteakHouse | 1.100 | 1.100 | NULL | 0.000 | 0.000 | NULL |
| Riz blanc | 5.000 | 5.000 | 5.000 | 0.000 | 0.000 | 0.000 |
| Yahourts Vanille | 0.000 | 0.000 | NULL | 0.100 | 0.100 | NULL |
+-----------------------------+---------+---------+---------+-----------+-----------+-----------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager