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
| --------------
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 `total`
--------------
--------------
CREATE TABLE `total`
( `typetarif` smallint unsigned NOT NULL primary key,
`index1` decimal(15,2) NOT NULL,
`index2` decimal(15,2) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `total` (`typetarif`,`index1`,`index2`) values
(22, 11.0, 89.0),
(33, 25.0, 75.0),
(55, 47.0, 53.0),
(77, 33.0, 77.0)
--------------
--------------
select * from total
--------------
+-----------+--------+--------+
| typetarif | index1 | index2 |
+-----------+--------+--------+
| 22 | 11.00 | 89.00 |
| 33 | 25.00 | 75.00 |
| 55 | 47.00 | 53.00 |
| 77 | 33.00 | 77.00 |
+-----------+--------+--------+
--------------
DROP TABLE IF EXISTS `commune`
--------------
--------------
CREATE TABLE `commune`
( `clef` integer unsigned NOT NULL primary key,
`typetarif` smallint unsigned NOT NULL,
`montant` decimal(15,2) NOT NULL,
`calcul` decimal(15,2) NOT NULL,
`annee` smallint unsigned NOT NULL,
`formule` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `commune` (`clef`,`typetarif`,`montant`,`calcul`,`annee`,`formule`) values
(1, 22, 1200.0, 1200.0, 2016, 'test'),
(2, 33, 800.0, 800.0, 2016, 'test_2'),
(3, 55, 1500.0, 1500.0, 2016, 'test'),
(4, 77, 400.0, 400.0, 2016, 'test_2')
--------------
--------------
select * from commune
--------------
+------+-----------+---------+---------+-------+---------+
| clef | typetarif | montant | calcul | annee | formule |
+------+-----------+---------+---------+-------+---------+
| 1 | 22 | 1200.00 | 1200.00 | 2016 | test |
| 2 | 33 | 800.00 | 800.00 | 2016 | test_2 |
| 3 | 55 | 1500.00 | 1500.00 | 2016 | test |
| 4 | 77 | 400.00 | 400.00 | 2016 | test_2 |
+------+-----------+---------+---------+-------+---------+
--------------
DROP TABLE IF EXISTS `archive`
--------------
--------------
CREATE TABLE `archive`
( `clef` integer unsigned NOT NULL primary key,
`montant` decimal(15,2) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `archive` (`clef`,`montant`) values
(1, 1500.0), (4, 400.0)
--------------
--------------
select * from archive
--------------
+------+---------+
| clef | montant |
+------+---------+
| 1 | 1500.00 |
| 4 | 400.00 |
+------+---------+
--------------
select c.clef, 10 as type, null as montant
from commune as c
where not exists ( select 1 from archive as a where a.clef = c.clef )
union
select c.clef, 11 as type, null as montant
from commune as c
where exists ( select 1 from archive as a where a.clef = c.clef and a.montant != c.montant )
union
select c.clef, null as type, cast((c.calcul * (t.index1 * 0.2) + (t.index2 * 0.8)) as decimal(15,2)) as montant
from commune as c
inner join total as t
on t.typetarif = c.typetarif
where c.annee = 2016
and c.formule = 'test'
union
select c.clef, null as type, cast((c.calcul * (t.index1 * 0.2)) as decimal(15,2)) as montant
from commune as c
inner join total as t
on t.typetarif = c.typetarif
where c.annee = 2016
and c.formule = 'test_2'
order by clef
--------------
+------+------+----------+
| clef | type | montant |
+------+------+----------+
| 1 | 11 | NULL |
| 1 | NULL | 2711.20 |
| 2 | 10 | NULL |
| 2 | NULL | 4000.00 |
| 3 | 10 | NULL |
| 3 | NULL | 14142.40 |
| 4 | NULL | 2640.00 |
+------+------+----------+
--------------
select clef, max(type) as type, max(montant) as montant
from (
select c.clef, 10 as type, null as montant
from commune as c
where not exists ( select 1 from archive as a where a.clef = c.clef )
union
select c.clef, 11 as type, null as montant
from commune as c
where exists ( select 1 from archive as a where a.clef = c.clef and a.montant != c.montant )
union
select c.clef, null as type, cast((c.calcul * (t.index1 * 0.2) + (t.index2 * 0.8)) as decimal(15,2)) as montant
from commune as c
inner join total as t
on t.typetarif = c.typetarif
where c.annee = 2016
and c.formule = 'test'
union
select c.clef, null as type, cast((c.calcul * (t.index1 * 0.2)) as decimal(15,2)) as montant
from commune as c
inner join total as t
on t.typetarif = c.typetarif
where c.annee = 2016
and c.formule = 'test_2'
) as x
group by clef
having type is not null
order by clef
--------------
+------+------+----------+
| clef | type | montant |
+------+------+----------+
| 1 | 11 | 2711.20 |
| 2 | 10 | 4000.00 |
| 3 | 10 | 14142.40 |
+------+------+----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager