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
| --------------
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 `formation`
--------------
--------------
CREATE TABLE `formation`
( `id` integer unsigned NOT NULL auto_increment primary key,
`libelle` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `formation` (`libelle`) values
('un'),('deux'),('trois'),('quatre'),('cinq'),('six'),('sept')
--------------
--------------
select * from formation
--------------
+----+---------+
| id | libelle |
+----+---------+
| 1 | un |
| 2 | deux |
| 3 | trois |
| 4 | quatre |
| 5 | cinq |
| 6 | six |
| 7 | sept |
+----+---------+
--------------
DROP TABLE IF EXISTS `intervenant`
--------------
--------------
CREATE TABLE `intervenant`
( `id` integer unsigned NOT NULL auto_increment primary key,
`nom` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `intervenant` (`nom`) values
('andré'),('pierre'),('marcel'),('paul'),('jacques'),('léon'),('pierre'),('françois'),('thierry'),('eric')
--------------
--------------
select * from intervenant
--------------
+----+----------+
| id | nom |
+----+----------+
| 1 | andré |
| 2 | pierre |
| 3 | marcel |
| 4 | paul |
| 5 | jacques |
| 6 | léon |
| 7 | pierre |
| 8 | françois |
| 9 | thierry |
| 10 | eric |
+----+----------+
--------------
DROP TABLE IF EXISTS `association`
--------------
--------------
CREATE TABLE `association`
( `id` integer unsigned NOT NULL auto_increment primary key,
`id_formation` integer unsigned NOT NULL,
`id_intervenant` integer unsigned NOT NULL,
CONSTRAINT `FK_FORMATION` FOREIGN KEY (`id_formation`) REFERENCES `formation` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_INTERVENANT` FOREIGN KEY (`id_intervenant`) REFERENCES `intervenant` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `association` (`id_formation`,`id_intervenant`) values
(1, 1),(1, 2),(1, 3),
(2, 2),(2, 4),
(3, 5),
(4, 3),(4, 6),(4, 7),
(5, 1),(5, 3),(5, 8),
(6, 6),(6, 7),
(7, 9),(7,10)
--------------
--------------
select * from association
--------------
+----+--------------+----------------+
| id | id_formation | id_intervenant |
+----+--------------+----------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 2 |
| 5 | 2 | 4 |
| 6 | 3 | 5 |
| 7 | 4 | 3 |
| 8 | 4 | 6 |
| 9 | 4 | 7 |
| 10 | 5 | 1 |
| 11 | 5 | 3 |
| 12 | 5 | 8 |
| 13 | 6 | 6 |
| 14 | 6 | 7 |
| 15 | 7 | 9 |
| 16 | 7 | 10 |
+----+--------------+----------------+
--------------
select id,
libelle,
max(nom1) as nom1,
max(nom2) as nom2,
max(nom3) as nom3
from (
select id,
libelle,
case rang when 1 then x.nom else '.' end as nom1,
case rang when 2 then x.nom else '.' end as nom2,
case rang when 3 then x.nom else '.' end as nom3
from (
select f.id,
f.libelle,
count(case when i.nom > j.nom then 1 else null end) + 1 as rang,
i.nom
from association as a
inner join formation as f
on f.id = a.id_formation
left outer join intervenant as i
on i.id = a.id_intervenant
inner join association as b
on b.id_formation = a.id_formation
left outer join intervenant as j
on j.id = b.id_intervenant
group by f.id, f.libelle, i.nom
order by f.id, f.libelle, i.nom
) as x
) as y
group by id,libelle
--------------
+----+---------+---------+----------+--------+
| id | libelle | nom1 | nom2 | nom3 |
+----+---------+---------+----------+--------+
| 1 | un | andré | marcel | pierre |
| 2 | deux | paul | pierre | . |
| 3 | trois | jacques | . | . |
| 4 | quatre | léon | marcel | pierre |
| 5 | cinq | andré | françois | marcel |
| 6 | six | léon | pierre | . |
| 7 | sept | eric | thierry | . |
+----+---------+---------+----------+--------+
--------------
select f.*,i.*,j.*,k.*
from (select id0,
max(id1) as id1,
max(id2) as id2,
max(id3) as id3
from (select id_formation as id0,
case rang when 1 then id_intervenant else NULL end as id1,
case rang when 2 then id_intervenant else NULL end as id2,
case rang when 3 then id_intervenant else NULL end as id3
from (select a.id_formation,
count(case when a.id_intervenant > b.id_intervenant then 1 else null end) + 1 as rang,
a.id_intervenant
from association as a
inner join association as b
on b.id_formation = a.id_formation
group by a.id_formation, a.id_intervenant
) as x
group by id_formation, id_intervenant
) as y
group by id0
) as z
inner join formation as f
on f.id = z.id0
left outer join intervenant as i
on i.id = z.id1
left outer join intervenant as j
on j.id = z.id2
left outer join intervenant as k
on k.id = z.id3
--------------
+----+---------+------+---------+------+--------+------+----------+
| id | libelle | id | nom | id | nom | id | nom |
+----+---------+------+---------+------+--------+------+----------+
| 1 | un | 1 | andré | 2 | pierre | 3 | marcel |
| 2 | deux | 2 | pierre | 4 | paul | NULL | NULL |
| 3 | trois | 5 | jacques | NULL | NULL | NULL | NULL |
| 4 | quatre | 3 | marcel | 6 | léon | 7 | pierre |
| 5 | cinq | 1 | andré | 3 | marcel | 8 | françois |
| 6 | six | 6 | léon | 7 | pierre | NULL | NULL |
| 7 | sept | 9 | thierry | 10 | eric | NULL | NULL |
+----+---------+------+---------+------+--------+------+----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager