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
| --------------
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 `tabone`
--------------
--------------
CREATE TABLE `tabone`
( `id` integer unsigned auto_increment not null primary key,
`operator_id` smallint unsigned not null,
`planning_start_time` datetime not null,
`planning_end_time` datetime not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tabone` (`operator_id`,`planning_start_time`,`planning_end_time`) values
(2, '2017-11-15 07:00:00', '2017-11-15 17:00:00'),
(2, '2017-11-16 07:00:00', '2017-11-16 17:00:00'),
(2, '2017-11-17 07:00:00', '2017-11-17 17:00:00')
--------------
--------------
select * from `tabone`
--------------
+----+-------------+---------------------+---------------------+
| id | operator_id | planning_start_time | planning_end_time |
+----+-------------+---------------------+---------------------+
| 1 | 2 | 2017-11-15 07:00:00 | 2017-11-15 17:00:00 |
| 2 | 2 | 2017-11-16 07:00:00 | 2017-11-16 17:00:00 |
| 3 | 2 | 2017-11-17 07:00:00 | 2017-11-17 17:00:00 |
+----+-------------+---------------------+---------------------+
--------------
DROP TABLE IF EXISTS `tabtwo`
--------------
--------------
CREATE TABLE `tabtwo`
( `id` integer unsigned auto_increment not null primary key,
`operator_id` smallint unsigned not null,
`busy_start` datetime not null,
`busy_end` datetime not null,
`description` varchar(255) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `tabtwo` (`operator_id`,`busy_start`,`busy_end`,`description`) values
(2, '2017-11-16 08:00:00', '2017-11-16 12:00:00','Fraisage'),
(2, '2017-11-16 14:00:00', '2017-11-16 16:00:00','Manutention')
--------------
--------------
select * from `tabtwo`
--------------
+----+-------------+---------------------+---------------------+-------------+
| id | operator_id | busy_start | busy_end | description |
+----+-------------+---------------------+---------------------+-------------+
| 1 | 2 | 2017-11-16 08:00:00 | 2017-11-16 12:00:00 | Fraisage |
| 2 | 2 | 2017-11-16 14:00:00 | 2017-11-16 16:00:00 | Manutention |
+----+-------------+---------------------+---------------------+-------------+
--------------
DROP TABLE IF EXISTS `trav`
--------------
--------------
CREATE TABLE `trav`
( `id` integer unsigned auto_increment not null primary key,
`operator_id` smallint unsigned not null,
`available_start_time` datetime not null,
`available_end_time` datetime not null,
`available_duration` integer unsigned not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
DROP PROCEDURE IF EXISTS `remplir`
--------------
--------------
CREATE PROCEDURE `remplir` ()
DETERMINISTIC
NO SQL
BEGIN
DECLARE _operator smallint default NULL;
DECLARE _plandeb datetime default NULL;
DECLARE _planfin datetime default NULL;
DECLARE _busydeb datetime default NULL;
DECLARE _busyfin datetime default NULL;
DECLARE _precope smallint default NULL;
DECLARE _precfin datetime default NULL;
DECLARE _avaideb datetime default NULL;
DECLARE _avaifin datetime default NULL;
DECLARE _duration integer default NULL;
DECLARE _rupt1 varchar(255) default NULL;
DECLARE _rupt2 tinyint default NULL;
DECLARE _fin INTEGER DEFAULT 1;
DECLARE _tab CURSOR FOR SELECT t1.operator_id, t1.planning_start_time, t1.planning_end_time, t2.busy_start, t2.busy_end
from `tabone` as t1
left outer join `tabtwo` as t2
on t2.operator_id = t1.operator_id
and t2.busy_start >= t1.planning_start_time
and t2.busy_end <= t1.planning_end_time
order by t1.operator_id, t1.planning_start_time, t2.busy_start;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _fin = 0;
OPEN _tab;
FETCH _tab INTO _operator, _plandeb, _planfin, _busydeb, _busyfin;
SET _rupt1 = concat(_operator,date(_plandeb));
SET _rupt2 = 1;
SET _precope = _operator;
SET _avaideb = _plandeb;
SET _precfin = _planfin;
WHILE (_fin)
DO
SELECT _operator, _plandeb, _planfin, _busydeb, _busyfin;
IF _busydeb is null THEN
SET _duration = time_to_sec(timediff(_planfin,_plandeb)) / 60;
INSERT INTO `trav` (`operator_id`,`available_start_time`,`available_end_time`,`available_duration`) VALUES (_operator, _plandeb, _planfin, _duration);
SET _rupt2 = 0;
ELSE
SET _avaifin = _busydeb;
SET _duration = time_to_sec(timediff(_avaifin,_avaideb)) / 60;
INSERT INTO `trav` (`operator_id`,`available_start_time`,`available_end_time`,`available_duration`) VALUES (_operator, _avaideb, _avaifin, _duration);
SET _avaideb = _busyfin;
END IF;
FETCH _tab INTO _operator, _plandeb, _planfin, _busydeb, _busyfin;
IF _rupt1 <> concat(_operator,date(_plandeb)) THEN
IF _rupt2 THEN
SET _duration = time_to_sec(timediff(_precfin,_avaideb)) / 60;
INSERT INTO `trav` (`operator_id`,`available_start_time`,`available_end_time`,`available_duration`) values (_precope, _avaideb, _precfin, _duration);
END IF;
SET _precope = _operator;
SET _avaideb = _plandeb;
SET _precfin = _planfin;
END IF;
SET _rupt1 = concat(_operator,date(_plandeb));
SET _rupt2 = 1;
END WHILE;
CLOSE _tab;
END
--------------
--------------
call `remplir` ()
--------------
+-----------+---------------------+---------------------+----------+----------+
| _operator | _plandeb | _planfin | _busydeb | _busyfin |
+-----------+---------------------+---------------------+----------+----------+
| 2 | 2017-11-15 07:00:00 | 2017-11-15 17:00:00 | NULL | NULL |
+-----------+---------------------+---------------------+----------+----------+
+-----------+---------------------+---------------------+---------------------+---------------------+
| _operator | _plandeb | _planfin | _busydeb | _busyfin |
+-----------+---------------------+---------------------+---------------------+---------------------+
| 2 | 2017-11-16 07:00:00 | 2017-11-16 17:00:00 | 2017-11-16 08:00:00 | 2017-11-16 12:00:00 |
+-----------+---------------------+---------------------+---------------------+---------------------+
+-----------+---------------------+---------------------+---------------------+---------------------+
| _operator | _plandeb | _planfin | _busydeb | _busyfin |
+-----------+---------------------+---------------------+---------------------+---------------------+
| 2 | 2017-11-16 07:00:00 | 2017-11-16 17:00:00 | 2017-11-16 14:00:00 | 2017-11-16 16:00:00 |
+-----------+---------------------+---------------------+---------------------+---------------------+
+-----------+---------------------+---------------------+----------+----------+
| _operator | _plandeb | _planfin | _busydeb | _busyfin |
+-----------+---------------------+---------------------+----------+----------+
| 2 | 2017-11-17 07:00:00 | 2017-11-17 17:00:00 | NULL | NULL |
+-----------+---------------------+---------------------+----------+----------+
--------------
select t1.operator_id,
t1.planning_start_time,
t1.planning_end_time,
t2.busy_start,
t2.busy_end
from `tabone` as t1
left outer join `tabtwo` as t2
on t2.operator_id = t1.operator_id
and t2.busy_start >= t1.planning_start_time
and t2.busy_end <= t1.planning_end_time
order by t1.operator_id, t1.planning_start_time, t2.busy_start
--------------
+-------------+---------------------+---------------------+---------------------+---------------------+
| operator_id | planning_start_time | planning_end_time | busy_start | busy_end |
+-------------+---------------------+---------------------+---------------------+---------------------+
| 2 | 2017-11-15 07:00:00 | 2017-11-15 17:00:00 | NULL | NULL |
| 2 | 2017-11-16 07:00:00 | 2017-11-16 17:00:00 | 2017-11-16 08:00:00 | 2017-11-16 12:00:00 |
| 2 | 2017-11-16 07:00:00 | 2017-11-16 17:00:00 | 2017-11-16 14:00:00 | 2017-11-16 16:00:00 |
| 2 | 2017-11-17 07:00:00 | 2017-11-17 17:00:00 | NULL | NULL |
+-------------+---------------------+---------------------+---------------------+---------------------+
--------------
select * from `trav`
--------------
+----+-------------+----------------------+---------------------+--------------------+
| id | operator_id | available_start_time | available_end_time | available_duration |
+----+-------------+----------------------+---------------------+--------------------+
| 1 | 2 | 2017-11-15 07:00:00 | 2017-11-15 17:00:00 | 600 |
| 2 | 2 | 2017-11-16 07:00:00 | 2017-11-16 08:00:00 | 60 |
| 3 | 2 | 2017-11-16 12:00:00 | 2017-11-16 14:00:00 | 120 |
| 4 | 2 | 2017-11-16 16:00:00 | 2017-11-16 17:00:00 | 60 |
| 5 | 2 | 2017-11-17 07:00:00 | 2017-11-17 17:00:00 | 600 |
+----+-------------+----------------------+---------------------+--------------------+
--------------
select *
from `trav`
where available_start_time >= '2017-11-16 00:00:00'
and available_end_time < '2017-11-17 00:00:00'
and available_duration >= 60
--------------
+----+-------------+----------------------+---------------------+--------------------+
| id | operator_id | available_start_time | available_end_time | available_duration |
+----+-------------+----------------------+---------------------+--------------------+
| 2 | 2 | 2017-11-16 07:00:00 | 2017-11-16 08:00:00 | 60 |
| 3 | 2 | 2017-11-16 12:00:00 | 2017-11-16 14:00:00 | 120 |
| 4 | 2 | 2017-11-16 16:00:00 | 2017-11-16 17:00:00 | 60 |
+----+-------------+----------------------+---------------------+--------------------+
--------------
select *
from `trav` as t1
where t1.id = ( select min(id)
from `trav` as t2
where t2.available_start_time >= '2017-11-16 00:00:00'
and t2.available_end_time < '2017-11-17 00:00:00'
and t2.available_duration >= 60
and t2.operator_id = t1.operator_id
)
--------------
+----+-------------+----------------------+---------------------+--------------------+
| id | operator_id | available_start_time | available_end_time | available_duration |
+----+-------------+----------------------+---------------------+--------------------+
| 2 | 2 | 2017-11-16 07:00:00 | 2017-11-16 08:00:00 | 60 |
+----+-------------+----------------------+---------------------+--------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager