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
| --------------
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,
`date_jour` date NOT NULL,
`heure_debut` time NOT NULL,
`heure_fin` time NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `test` (`date_jour`, `heure_debut`,`heure_fin`) VALUES
('2016-05-01', '04:00:00', '11:00:00'),
('2016-05-01', '15:00:00', '18:00:00'),
('2016-05-02', '04:00:00', '11:00:00'),
('2016-05-02', '15:00:00', '18:00:00'),
('2016-05-03', '04:00:00', '11:00:00'),
('2016-05-02', '15:00:00', '18:00:00'),
('2016-05-04', '06:00:00', '08:30:00'),
('2016-05-04', '10:45:00', '11:25:00'),
('2016-05-05', '04:00:00', '11:00:00'),
('2016-05-05', '15:00:00', '18:00:00'),
('2016-05-06', '04:00:00', '11:00:00'),
('2016-05-06', '15:00:00', '18:00:00'),
('2016-05-07', '04:00:00', '11:00:00'),
('2016-05-07', '15:00:00', '18:00:00'),
('2016-05-08', '04:00:00', '11:00:00'),
('2016-05-08', '15:00:00', '18:00:00'),
('2016-05-09', '04:00:00', '11:00:00'),
('2016-05-09', '15:00:00', '18:00:00'),
('2016-05-10', '04:00:00', '11:00:00'),
('2016-05-10', '15:00:00', '18:00:00'),
('2016-05-31', '11:00:00', '13:30:00'),
('2016-05-31', '15:00:00', '17:00:00')
--------------
--------------
select * from test
--------------
+----+------------+-------------+-----------+
| id | date_jour | heure_debut | heure_fin |
+----+------------+-------------+-----------+
| 1 | 2016-05-01 | 04:00:00 | 11:00:00 |
| 2 | 2016-05-01 | 15:00:00 | 18:00:00 |
| 3 | 2016-05-02 | 04:00:00 | 11:00:00 |
| 4 | 2016-05-02 | 15:00:00 | 18:00:00 |
| 5 | 2016-05-03 | 04:00:00 | 11:00:00 |
| 6 | 2016-05-02 | 15:00:00 | 18:00:00 |
| 7 | 2016-05-04 | 06:00:00 | 08:30:00 |
| 8 | 2016-05-04 | 10:45:00 | 11:25:00 |
| 9 | 2016-05-05 | 04:00:00 | 11:00:00 |
| 10 | 2016-05-05 | 15:00:00 | 18:00:00 |
| 11 | 2016-05-06 | 04:00:00 | 11:00:00 |
| 12 | 2016-05-06 | 15:00:00 | 18:00:00 |
| 13 | 2016-05-07 | 04:00:00 | 11:00:00 |
| 14 | 2016-05-07 | 15:00:00 | 18:00:00 |
| 15 | 2016-05-08 | 04:00:00 | 11:00:00 |
| 16 | 2016-05-08 | 15:00:00 | 18:00:00 |
| 17 | 2016-05-09 | 04:00:00 | 11:00:00 |
| 18 | 2016-05-09 | 15:00:00 | 18:00:00 |
| 19 | 2016-05-10 | 04:00:00 | 11:00:00 |
| 20 | 2016-05-10 | 15:00:00 | 18:00:00 |
| 21 | 2016-05-31 | 11:00:00 | 13:30:00 |
| 22 | 2016-05-31 | 15:00:00 | 17:00:00 |
+----+------------+-------------+-----------+
--------------
select date_jour as jour,
timediff(max(heure_fin), min(heure_debut)) as amplitude,
sec_to_time(sum(time_to_sec(timediff(heure_fin, heure_debut)))) as total
from test
group by date_jour
--------------
+------------+-----------+----------+
| jour | amplitude | total |
+------------+-----------+----------+
| 2016-05-01 | 14:00:00 | 10:00:00 |
| 2016-05-02 | 14:00:00 | 13:00:00 |
| 2016-05-03 | 07:00:00 | 07:00:00 |
| 2016-05-04 | 05:25:00 | 03:10:00 |
| 2016-05-05 | 14:00:00 | 10:00:00 |
| 2016-05-06 | 14:00:00 | 10:00:00 |
| 2016-05-07 | 14:00:00 | 10:00:00 |
| 2016-05-08 | 14:00:00 | 10:00:00 |
| 2016-05-09 | 14:00:00 | 10:00:00 |
| 2016-05-10 | 14:00:00 | 10:00:00 |
| 2016-05-31 | 06:00:00 | 04:30:00 |
+------------+-----------+----------+
--------------
select date_format(date_jour, '%Y-%v') as semaine,
sec_to_time(sum(time_to_sec(amplitude))) as amplitude,
sec_to_time(sum(time_to_sec(total))) as total
from (
select date_jour,
timediff( max(heure_fin), min(heure_debut)) as amplitude,
timediff(any_value(heure_fin), any_value(heure_debut)) as total
from test
group by date_jour
) as x
group by semaine
--------------
+---------+-----------+----------+
| semaine | amplitude | total |
+---------+-----------+----------+
| 2016-17 | 14:00:00 | 07:00:00 |
| 2016-18 | 82:25:00 | 44:30:00 |
| 2016-19 | 28:00:00 | 14:00:00 |
| 2016-22 | 06:00:00 | 02:30:00 |
+---------+-----------+----------+
--------------
select date_format(date_jour, '%Y-%m') as mois,
sec_to_time(sum(time_to_sec(amplitude))) as amplitude,
sec_to_time(sum(time_to_sec(total))) as total
from (
select date_jour,
timediff( max(heure_fin), min(heure_debut)) as amplitude,
timediff(any_value(heure_fin), any_value(heure_debut)) as total
from test
group by date_jour
) as x
group by mois
--------------
+---------+-----------+----------+
| mois | amplitude | total |
+---------+-----------+----------+
| 2016-05 | 130:25:00 | 68:00:00 |
+---------+-----------+----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager