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
| --------------
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_1` date not null,
`date_2` date not null,
`date_5` date not null,
`duree` integer unsigned not null,
`valeur1` integer unsigned not null,
`statut` varchar(255) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`date_1`,`date_2`,`date_5`,`duree`,`valeur1`,`statut`) values
('2016-12-31','2015-12-31','2015-06-01',3, 900, 'réel'),
('2016-12-31','2015-12-31','2015-08-25',2, 900, 'réel'),
('2016-12-31','2014-12-31','2015-02-04',5, 900, 'réel'),
('2016-12-31','2015-12-31','2015-02-04',3, 900, 'réel'),
('2016-12-31','2016-12-31','2016-10-06',1, 900, 'réel'),
('2016-12-31','2016-12-31','2016-10-14',1, 900, 'réel')
--------------
--------------
select * from `test`
--------------
+----+------------+------------+------------+-------+---------+--------+
| id | date_1 | date_2 | date_5 | duree | valeur1 | statut |
+----+------------+------------+------------+-------+---------+--------+
| 1 | 2016-12-31 | 2015-12-31 | 2015-06-01 | 3 | 900 | réel |
| 2 | 2016-12-31 | 2015-12-31 | 2015-08-25 | 2 | 900 | réel |
| 3 | 2016-12-31 | 2014-12-31 | 2015-02-04 | 5 | 900 | réel |
| 4 | 2016-12-31 | 2015-12-31 | 2015-02-04 | 3 | 900 | réel |
| 5 | 2016-12-31 | 2016-12-31 | 2016-10-06 | 1 | 900 | réel |
| 6 | 2016-12-31 | 2016-12-31 | 2016-10-14 | 1 | 900 | réel |
+----+------------+------------+------------+-------+---------+--------+
--------------
select nbjour,
(cast(datediff(if(val2 < val1, val2, val1), date_5) as decimal(15,2)) * 100) / nbjour as calc
from ( select datediff(val1, date_2) as nbjour,
if(date_1 < comp, date_1, comp) as val2,
val1, date_5
from ( select '2016-12-31' as comp,
date_2 + interval duree year as val1,
ifnull(date_1, '9999-12-31') as date_1,
date_2,
date_5
from `test`
where `valeur1` >= 800
and `statut` = 'réel'
having `date_2` <= comp
) as x
) as y
--------------
+--------+-----------+
| nbjour | calc |
+--------+-----------+
| 1096 | 52.828467 |
| 731 | 67.578659 |
| 1826 | 38.116101 |
| 1096 | 63.503650 |
| 365 | 23.561644 |
| 365 | 21.369863 |
+--------+-----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager