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
| --------------
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 `adresse`
--------------
--------------
create table `adresse`
( `id` integer unsigned not null auto_increment primary key,
`departement` smallint unsigned not null,
`adresse` text not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `adresse` (`departement`,`adresse`) VALUES
(94, 'rue machin'),
(94, 'rue bidule'),
(75, 'av général'),
(75, 'route pouet')
--------------
--------------
select * from `adresse`
--------------
+----+-------------+-------------+
| id | departement | adresse |
+----+-------------+-------------+
| 1 | 94 | rue machin |
| 2 | 94 | rue bidule |
| 3 | 75 | av général |
| 4 | 75 | route pouet |
+----+-------------+-------------+
--------------
DROP TABLE IF EXISTS `machine`
--------------
--------------
create table `machine`
( `id` integer unsigned not null auto_increment primary key,
`statut` varchar(255) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `machine` (`statut`) VALUES
('machine indemne'),('machine hs'),('machine à réparrer'),('machine en cours de réparation')
--------------
--------------
select * from `machine`
--------------
+----+--------------------------------+
| id | statut |
+----+--------------------------------+
| 1 | machine indemne |
| 2 | machine hs |
| 3 | machine à réparrer |
| 4 | machine en cours de réparation |
+----+--------------------------------+
--------------
DROP TABLE IF EXISTS `truc_chose`
--------------
--------------
create table `truc_chose`
( `id` integer unsigned not null auto_increment primary key,
`clef_adresse` integer unsigned not null,
`clef_machine` integer unsigned not null,
CONSTRAINT `FK_ADRESSE` FOREIGN KEY (`clef_adresse`) REFERENCES `adresse` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_MACHINE` FOREIGN KEY (`clef_machine`) REFERENCES `machine` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `truc_chose` (`clef_adresse`,`clef_machine`) VALUES
(1, 1),(1, 2),(1, 3),(1, 1),
(2, 1),(2, 2),(2, 4),
(3, 2),
(4, 1),(4, 2)
--------------
--------------
select * from `truc_chose`
--------------
+----+--------------+--------------+
| id | clef_adresse | clef_machine |
+----+--------------+--------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 1 |
| 5 | 2 | 1 |
| 6 | 2 | 2 |
| 7 | 2 | 4 |
| 8 | 3 | 2 |
| 9 | 4 | 1 |
| 10 | 4 | 2 |
+----+--------------+--------------+
--------------
select *
from `truc_chose` as t1
inner join `adresse` as t2
on t2.id = t1.clef_adresse
inner join `machine` as t3
on t3.id = t1.clef_machine
--------------
+----+--------------+--------------+----+-------------+-------------+----+--------------------------------+
| id | clef_adresse | clef_machine | id | departement | adresse | id | statut |
+----+--------------+--------------+----+-------------+-------------+----+--------------------------------+
| 1 | 1 | 1 | 1 | 94 | rue machin | 1 | machine indemne |
| 2 | 1 | 2 | 1 | 94 | rue machin | 2 | machine hs |
| 3 | 1 | 3 | 1 | 94 | rue machin | 3 | machine à réparrer |
| 4 | 1 | 1 | 1 | 94 | rue machin | 1 | machine indemne |
| 5 | 2 | 1 | 2 | 94 | rue bidule | 1 | machine indemne |
| 6 | 2 | 2 | 2 | 94 | rue bidule | 2 | machine hs |
| 7 | 2 | 4 | 2 | 94 | rue bidule | 4 | machine en cours de réparation |
| 8 | 3 | 2 | 3 | 75 | av général | 2 | machine hs |
| 9 | 4 | 1 | 4 | 75 | route pouet | 1 | machine indemne |
| 10 | 4 | 2 | 4 | 75 | route pouet | 2 | machine hs |
+----+--------------+--------------+----+-------------+-------------+----+--------------------------------+
--------------
select t2.departement,
count(t2.departement) as nbre_adresse,
sum(case t3.id when 1 then 1 else 0 end) as nbr_machine_indemne,
sum(case t3.id when 2 then 1 else 0 end) as nbr_machine_hs,
sum(case t3.id when 3 then 1 else 0 end) as nbr_machine_a_reparre,
sum(case t3.id when 4 then 1 else 0 end) as nbr_machine_en_cours_de_reparation
from `truc_chose` as t1
inner join `adresse` as t2
on t2.id = t1.clef_adresse
inner join `machine` as t3
on t3.id = t1.clef_machine
group by t2.departement
--------------
+-------------+--------------+---------------------+----------------+-----------------------+------------------------------------+
| departement | nbre_adresse | nbr_machine_indemne | nbr_machine_hs | nbr_machine_a_reparre | nbr_machine_en_cours_de_reparation |
+-------------+--------------+---------------------+----------------+-----------------------+------------------------------------+
| 94 | 7 | 3 | 2 | 1 | 1 |
| 75 | 3 | 1 | 2 | 0 | 0 |
+-------------+--------------+---------------------+----------------+-----------------------+------------------------------------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager