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
| --------------
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 `light1`
--------------
--------------
CREATE TABLE `light1`
( `id` integer unsigned not null auto_increment primary key,
`periode` timestamp not null,
`bouton` char(03) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `light1` (`periode`,`bouton`) VALUES
('2018-06-03 21:30:41.643','ON'),
('2018-06-03 00:46:09.557','OFF'),
('2018-06-03 00:44:36.262','ON'),
('2018-06-03 00:00:05.340','OFF'),
('2018-06-02 01:47:32.085','OFF'),
('2018-06-02 00:00:03.409','ON'),
('2018-06-01 20:45:57.259','ON'),
('2018-06-01 00:00:08.095','OFF'),
('2018-05-31 00:49:04.805','OFF'),
('2018-05-31 00:00:01.840','ON'),
('2018-05-30 20:17:14.215','ON'),
('2018-05-30 08:34:27.756','OFF'),
('2018-05-30 08:34:24.054','ON'),
('2018-05-30 02:18:21.658','OFF'),
('2018-05-30 00:00:01.882','ON'),
('2018-05-29 19:31:53.409','ON'),
('2018-05-29 01:35:10.740','OFF'),
('2018-05-29 00:00:01.957','ON')
--------------
--------------
select *
from `light1`
order by periode desc
--------------
+----+---------------------+--------+
| id | periode | bouton |
+----+---------------------+--------+
| 1 | 2018-06-03 21:30:42 | ON |
| 2 | 2018-06-03 00:46:10 | OFF |
| 3 | 2018-06-03 00:44:36 | ON |
| 4 | 2018-06-03 00:00:05 | OFF |
| 5 | 2018-06-02 01:47:32 | OFF |
| 6 | 2018-06-02 00:00:03 | ON |
| 7 | 2018-06-01 20:45:57 | ON |
| 8 | 2018-06-01 00:00:08 | OFF |
| 9 | 2018-05-31 00:49:05 | OFF |
| 10 | 2018-05-31 00:00:02 | ON |
| 11 | 2018-05-30 20:17:14 | ON |
| 12 | 2018-05-30 08:34:28 | OFF |
| 13 | 2018-05-30 08:34:24 | ON |
| 14 | 2018-05-30 02:18:22 | OFF |
| 15 | 2018-05-30 00:00:02 | ON |
| 16 | 2018-05-29 19:31:53 | ON |
| 17 | 2018-05-29 01:35:11 | OFF |
| 18 | 2018-05-29 00:00:02 | ON |
+----+---------------------+--------+
--------------
drop view if exists `vue_1`
--------------
--------------
create view `vue_1` as
select t1.periode as debut,
min(t2.periode) as fin
from `light1` as t1
left outer join `light1` as t2
on t2.bouton = 'off'
and t2.periode > t1.periode
where t1.bouton = 'on'
group by t1.periode
order by t1.periode
--------------
--------------
select * from vue_1
--------------
+---------------------+---------------------+
| debut | fin |
+---------------------+---------------------+
| 2018-05-29 00:00:02 | 2018-05-29 01:35:11 |
| 2018-05-29 19:31:53 | 2018-05-30 02:18:22 |
| 2018-05-30 00:00:02 | 2018-05-30 02:18:22 |
| 2018-05-30 08:34:24 | 2018-05-30 08:34:28 |
| 2018-05-30 20:17:14 | 2018-05-31 00:49:05 |
| 2018-05-31 00:00:02 | 2018-05-31 00:49:05 |
| 2018-06-01 20:45:57 | 2018-06-02 01:47:32 |
| 2018-06-02 00:00:03 | 2018-06-02 01:47:32 |
| 2018-06-03 00:44:36 | 2018-06-03 00:46:10 |
| 2018-06-03 21:30:42 | NULL |
+---------------------+---------------------+
--------------
select debut as "debut on",
fin as "fin off",
timediff(fin,debut) as diff
from vue_1
--------------
+---------------------+---------------------+----------+
| debut on | fin off | diff |
+---------------------+---------------------+----------+
| 2018-05-29 00:00:02 | 2018-05-29 01:35:11 | 01:35:09 |
| 2018-05-29 19:31:53 | 2018-05-30 02:18:22 | 06:46:29 |
| 2018-05-30 00:00:02 | 2018-05-30 02:18:22 | 02:18:20 |
| 2018-05-30 08:34:24 | 2018-05-30 08:34:28 | 00:00:04 |
| 2018-05-30 20:17:14 | 2018-05-31 00:49:05 | 04:31:51 |
| 2018-05-31 00:00:02 | 2018-05-31 00:49:05 | 00:49:03 |
| 2018-06-01 20:45:57 | 2018-06-02 01:47:32 | 05:01:35 |
| 2018-06-02 00:00:03 | 2018-06-02 01:47:32 | 01:47:29 |
| 2018-06-03 00:44:36 | 2018-06-03 00:46:10 | 00:01:34 |
| 2018-06-03 21:30:42 | NULL | NULL |
+---------------------+---------------------+----------+
--------------
select sec_to_time(sum(time_to_sec(timediff(fin,debut)))) as "diff on"
from vue_1
where debut between '2018-05-29 00:00:01' and '2018-06-03 00:46:09'
--------------
+----------+
| diff on |
+----------+
| 22:51:34 |
+----------+
--------------
drop view if exists `vue_2`
--------------
--------------
create view `vue_2` as
select t1.periode as debut,
min(t2.periode) as fin
from `light1` as t1
left outer join `light1` as t2
on t2.bouton = 'on'
and t2.periode > t1.periode
where t1.bouton = 'off'
group by t1.periode
order by t1.periode
--------------
--------------
select * from vue_2
--------------
+---------------------+---------------------+
| debut | fin |
+---------------------+---------------------+
| 2018-05-29 01:35:11 | 2018-05-29 19:31:53 |
| 2018-05-30 02:18:22 | 2018-05-30 08:34:24 |
| 2018-05-30 08:34:28 | 2018-05-30 20:17:14 |
| 2018-05-31 00:49:05 | 2018-06-01 20:45:57 |
| 2018-06-01 00:00:08 | 2018-06-01 20:45:57 |
| 2018-06-02 01:47:32 | 2018-06-03 00:44:36 |
| 2018-06-03 00:00:05 | 2018-06-03 00:44:36 |
| 2018-06-03 00:46:10 | 2018-06-03 21:30:42 |
+---------------------+---------------------+
--------------
select debut as "debut off",
fin as "fin on",
timediff(fin,debut) as diff
from vue_2
--------------
+---------------------+---------------------+----------+
| debut off | fin on | diff |
+---------------------+---------------------+----------+
| 2018-05-29 01:35:11 | 2018-05-29 19:31:53 | 17:56:42 |
| 2018-05-30 02:18:22 | 2018-05-30 08:34:24 | 06:16:02 |
| 2018-05-30 08:34:28 | 2018-05-30 20:17:14 | 11:42:46 |
| 2018-05-31 00:49:05 | 2018-06-01 20:45:57 | 43:56:52 |
| 2018-06-01 00:00:08 | 2018-06-01 20:45:57 | 20:45:49 |
| 2018-06-02 01:47:32 | 2018-06-03 00:44:36 | 22:57:04 |
| 2018-06-03 00:00:05 | 2018-06-03 00:44:36 | 00:44:31 |
| 2018-06-03 00:46:10 | 2018-06-03 21:30:42 | 20:44:32 |
+---------------------+---------------------+----------+
--------------
select sec_to_time(sum(time_to_sec(timediff(fin,debut)))) as "diff on"
from vue_2
where debut between '2018-05-29 01:35:10' and '2018-06-03 21:30:41'
--------------
+-----------+
| diff on |
+-----------+
| 145:04:18 |
+-----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager