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
| --------------
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 `equipement`
--------------
--------------
CREATE TABLE `equipement`
(
`id` integer unsigned NOT NULL AUTO_INCREMENT primary key,
`codeidentifiant` varchar(255) NOT NULL,
`periodicite` smallint unsigned NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `equipement` (`codeidentifiant`,`periodicite`) values
('MIV075', 6),
('MIV080', 6),
('MIV087', 12)
--------------
--------------
select * from equipement
--------------
+----+-----------------+-------------+
| id | codeidentifiant | periodicite |
+----+-----------------+-------------+
| 1 | MIV075 | 6 |
| 2 | MIV080 | 6 |
| 3 | MIV087 | 12 |
+----+-----------------+-------------+
--------------
DROP TABLE IF EXISTS `equipementaction`
--------------
--------------
CREATE TABLE `equipementaction`
(
`id` integer unsigned NOT NULL AUTO_INCREMENT primary key,
`codeidentifiant` varchar(255) NOT NULL,
`actiondate` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `equipementaction` (`codeidentifiant`,`actiondate`) values
('MIV075', '2015-11-01'),
('MIV075', '2015-10-01'),
('MIV075', '2015-09-01'),
('MIV080', '2016-07-01'),
('MIV080', '2016-06-01'),
('MIV080', '2016-05-01'),
('MIV080', '2016-04-01'),
('MIV080', '2016-03-01'),
('MIV080', '2016-02-01'),
('MIV087', '2016-04-01'),
('MIV087', '2016-03-03')
--------------
--------------
select * from equipementaction
--------------
+----+-----------------+------------+
| id | codeidentifiant | actiondate |
+----+-----------------+------------+
| 1 | MIV075 | 2015-11-01 |
| 2 | MIV075 | 2015-10-01 |
| 3 | MIV075 | 2015-09-01 |
| 4 | MIV080 | 2016-07-01 |
| 5 | MIV080 | 2016-06-01 |
| 6 | MIV080 | 2016-05-01 |
| 7 | MIV080 | 2016-04-01 |
| 8 | MIV080 | 2016-03-01 |
| 9 | MIV080 | 2016-02-01 |
| 10 | MIV087 | 2016-04-01 |
| 11 | MIV087 | 2016-03-03 |
+----+-----------------+------------+
--------------
select codeidentifiant,
date_sub(current_date, interval tb1.periodicite month) as date_min,
current_date as date_max
from equipement as tb1
order by codeidentifiant
--------------
+-----------------+------------+------------+
| codeidentifiant | date_min | date_max |
+-----------------+------------+------------+
| MIV075 | 2015-12-02 | 2016-06-02 |
| MIV080 | 2015-12-02 | 2016-06-02 |
| MIV087 | 2015-06-02 | 2016-06-02 |
+-----------------+------------+------------+
--------------
select tb1.codeidentifiant,
max(tb2.actiondate) as actiondate
from equipement as tb1
inner join equipementaction as tb2
ON tb2.codeidentifiant = tb1.codeidentifiant
where tb1.periodicite is not null
and tb2.actiondate between date_sub(current_date, interval tb1.periodicite month) and current_date
group by tb1.codeidentifiant
--------------
+-----------------+------------+
| codeidentifiant | actiondate |
+-----------------+------------+
| MIV080 | 2016-06-01 |
| MIV087 | 2016-04-01 |
+-----------------+------------+
--------------
select codeidentifiant,
count(*) as nbpresent
from (
select tb1.codeidentifiant,
max(tb2.actiondate) as actiondate
from equipement as tb1
inner join equipementaction as tb2
ON tb2.codeidentifiant = tb1.codeidentifiant
where tb1.periodicite is not null
and tb2.actiondate between date_sub(current_date, interval tb1.periodicite month) and current_date
group by tb1.codeidentifiant
) as x
group by codeidentifiant
order by codeidentifiant
--------------
+-----------------+-----------+
| codeidentifiant | nbpresent |
+-----------------+-----------+
| MIV080 | 1 |
| MIV087 | 1 |
+-----------------+-----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager