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
| --------------
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 `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`poste12` smallint unsigned not null,
`pompe` char(02) not null,
`etat` tinyint unsigned not null,
`time_on` datetime not null,
`time_off` datetime not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`id`,`poste12`,`pompe`,`etat`,`time_on`,`time_off`) values
(306, 0, 'p1', 1, '2017-02-02 10:53:10', '2017-02-02 10:53:41'),
(307, 0, 'p2', 1, '2017-02-02 10:54:00', '2017-02-02 10:54:18'),
(308, 0, 'p1', 1, '2017-02-02 10:54:35', '2017-02-02 10:54:55'),
(309, 0, 'p2', 1, '2017-02-02 10:54:40', '2017-02-02 10:54:55'),
(310, 0, 'p1', 1, '2017-02-02 23:59:40', '2017-02-03 00:00:25'),
(311, 0, 'p2', 1, '2017-02-02 23:58:40', '2017-02-03 00:01:10')
--------------
--------------
select * from `test`
--------------
+-----+---------+-------+------+---------------------+---------------------+
| id | poste12 | pompe | etat | time_on | time_off |
+-----+---------+-------+------+---------------------+---------------------+
| 306 | 0 | p1 | 1 | 2017-02-02 10:53:10 | 2017-02-02 10:53:41 |
| 307 | 0 | p2 | 1 | 2017-02-02 10:54:00 | 2017-02-02 10:54:18 |
| 308 | 0 | p1 | 1 | 2017-02-02 10:54:35 | 2017-02-02 10:54:55 |
| 309 | 0 | p2 | 1 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 |
| 310 | 0 | p1 | 1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 |
| 311 | 0 | p2 | 1 | 2017-02-02 23:58:40 | 2017-02-03 00:01:10 |
+-----+---------+-------+------+---------------------+---------------------+
--------------
drop view if exists `vue_1`
--------------
--------------
create view `vue_1` as
select *, cast(TIME_TO_SEC(TIMEDIFF(time_off, time_on)) as unsigned) as duration
from test
--------------
--------------
select * from `vue_1`
--------------
+-----+---------+-------+------+---------------------+---------------------+----------+
| id | poste12 | pompe | etat | time_on | time_off | duration |
+-----+---------+-------+------+---------------------+---------------------+----------+
| 306 | 0 | p1 | 1 | 2017-02-02 10:53:10 | 2017-02-02 10:53:41 | 31 |
| 307 | 0 | p2 | 1 | 2017-02-02 10:54:00 | 2017-02-02 10:54:18 | 18 |
| 308 | 0 | p1 | 1 | 2017-02-02 10:54:35 | 2017-02-02 10:54:55 | 20 |
| 309 | 0 | p2 | 1 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 | 15 |
| 310 | 0 | p1 | 1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 | 45 |
| 311 | 0 | p2 | 1 | 2017-02-02 23:58:40 | 2017-02-03 00:01:10 | 150 |
+-----+---------+-------+------+---------------------+---------------------+----------+
--------------
select sum(duration) as total from `vue_1`
--------------
+-------+
| total |
+-------+
| 279 |
+-------+
--------------
drop view if exists `vue_2`
--------------
--------------
create view `vue_2` as
select pompe, time_on, time_off, cast(TIME_TO_SEC(TIMEDIFF(time_off, time_on)) as unsigned) as duration
from ( select pompe,
time_on,
case when date(time_off) > date(time_on) then cast(concat(date(time_off), ' 00:00:00') as datetime) else time_off end as time_off
from test
union
select pompe,
cast(concat(date(time_off), ' 00:00:00') as datetime) as time_on,
time_off
from test
where date(time_off) > date(time_on)
) as x
--------------
--------------
select * from `vue_2`
--------------
+-------+---------------------+---------------------+----------+
| pompe | time_on | time_off | duration |
+-------+---------------------+---------------------+----------+
| p1 | 2017-02-02 10:53:10 | 2017-02-02 10:53:41 | 31 |
| p2 | 2017-02-02 10:54:00 | 2017-02-02 10:54:18 | 18 |
| p1 | 2017-02-02 10:54:35 | 2017-02-02 10:54:55 | 20 |
| p2 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 | 15 |
| p1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:00 | 20 |
| p2 | 2017-02-02 23:58:40 | 2017-02-03 00:00:00 | 80 |
| p1 | 2017-02-03 00:00:00 | 2017-02-03 00:00:25 | 25 |
| p2 | 2017-02-03 00:00:00 | 2017-02-03 00:01:10 | 70 |
+-------+---------------------+---------------------+----------+
--------------
select date(time_on) as jour,
pompe,
sum(duration) as duration
from vue_2
where time_on is not null
group by date(time_on), pompe
order by date(time_on), pompe
--------------
+------------+-------+----------+
| jour | pompe | duration |
+------------+-------+----------+
| 2017-02-02 | p1 | 71 |
| 2017-02-02 | p2 | 113 |
| 2017-02-03 | p1 | 25 |
| 2017-02-03 | p2 | 70 |
+------------+-------+----------+
--------------
select date_format(time_on, '%Y-%m') as mois,
pompe,
sum(duration) as duration
from vue_2
where time_on is not null
group by date_format(time_on, '%Y-%m'), pompe
order by date_format(time_on, '%Y-%m'), pompe
--------------
+---------+-------+----------+
| mois | pompe | duration |
+---------+-------+----------+
| 2017-02 | p1 | 96 |
| 2017-02 | p2 | 183 |
+---------+-------+----------+
--------------
select year(time_on) as annee,
pompe,
sum(duration) as duration
from vue_2
where time_on is not null
group by year(time_on), pompe
order by year(time_on), pompe
--------------
+-------+-------+----------+
| annee | pompe | duration |
+-------+-------+----------+
| 2017 | p1 | 96 |
| 2017 | p2 | 183 |
+-------+-------+----------+
--------------
drop view if exists `vue_3`
--------------
--------------
create view `vue_3` as
select *, case when fin > deb then cast(TIME_TO_SEC(TIMEDIFF(fin, deb)) as unsigned) else 0 end as duration
from ( select t1.id as p1_id,
t1.pompe as p1_pompe,
t1.time_on as p1_time_on,
t1.time_off as p1_time_off,
t2.id as p2_id,
t2.pompe as p2_pompe,
t2.time_on as p2_time_on,
t2.time_off as p2_time_off,
greatest(t1.time_on, t2.time_on) as deb,
least(t1.time_off, t2.time_off) as fin
from test as t1
inner join test as t2
on t2.pompe = 'p2'
and t2.time_on <= t1.time_off
where t1.pompe = 'p1'
order by t1.time_on, t2.time_on
) as x
--------------
--------------
select * from `vue_3`
--------------
+-------+----------+---------------------+---------------------+-------+----------+---------------------+---------------------+---------------------+---------------------+----------+
| p1_id | p1_pompe | p1_time_on | p1_time_off | p2_id | p2_pompe | p2_time_on | p2_time_off | deb | fin | duration |
+-------+----------+---------------------+---------------------+-------+----------+---------------------+---------------------+---------------------+---------------------+----------+
| 308 | p1 | 2017-02-02 10:54:35 | 2017-02-02 10:54:55 | 307 | p2 | 2017-02-02 10:54:00 | 2017-02-02 10:54:18 | 2017-02-02 10:54:35 | 2017-02-02 10:54:18 | 0 |
| 308 | p1 | 2017-02-02 10:54:35 | 2017-02-02 10:54:55 | 309 | p2 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 | 15 |
| 310 | p1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 | 307 | p2 | 2017-02-02 10:54:00 | 2017-02-02 10:54:18 | 2017-02-02 23:59:40 | 2017-02-02 10:54:18 | 0 |
| 310 | p1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 | 309 | p2 | 2017-02-02 10:54:40 | 2017-02-02 10:54:55 | 2017-02-02 23:59:40 | 2017-02-02 10:54:55 | 0 |
| 310 | p1 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 | 311 | p2 | 2017-02-02 23:58:40 | 2017-02-03 00:01:10 | 2017-02-02 23:59:40 | 2017-02-03 00:00:25 | 45 |
+-------+----------+---------------------+---------------------+-------+----------+---------------------+---------------------+---------------------+---------------------+----------+
--------------
select sum(duration) as duration
from vue_3
--------------
+----------+
| duration |
+----------+
| 60 |
+----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager