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
| --------------
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 `mystat`
--------------
--------------
CREATE TABLE `mystat`
( `client` integer unsigned NOT NULL,
`nom_client` varchar(255) NOT NULL,
`annee` smallint unsigned NOT NULL,
`mois` smallint unsigned NOT NULL,
`montant` decimal(15,2) NOT NULL,
primary key (`client`,`annee`,`mois`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `mystat` (`client`,`nom_client`,`annee`,`mois`,`montant`) values
(125, 'dupond', 2016, 1, 125.00),
(125, 'dupond', 2016, 2, 125.00),
(125, 'dupond', 2016, 3, 125.00),
(125, 'dupond', 2016, 4, 125.00),
(125, 'dupond', 2016, 5, 125.00),
(125, 'dupond', 2016, 6, 125.00),
(125, 'dupond', 2016, 7, 125.00),
(125, 'dupond', 2016, 8, 125.00),
(125, 'dupond', 2016, 9, 125.00),
(125, 'dupond', 2016, 10, 125.00),
(125, 'dupond', 2016, 11, 125.00),
(125, 'dupond', 2016, 12, 125.00),
(217, 'durand', 2016, 1, 33.00),
(217, 'durand', 2016, 2, 33.00),
(217, 'durand', 2016, 3, 33.00),
(217, 'durand', 2016, 4, 33.00),
(217, 'durand', 2016, 5, 33.00),
(217, 'durand', 2016, 6, 33.00),
(217, 'durand', 2016, 7, 33.00),
(217, 'durand', 2016, 8, 33.00),
(217, 'durand', 2016, 9, 33.00),
(217, 'durand', 2016, 10, 33.00),
(217, 'durand', 2016, 11, 33.00),
(217, 'durand', 2016, 12, 33.00)
--------------
--------------
select * from mystat
--------------
+--------+------------+-------+------+---------+
| client | nom_client | annee | mois | montant |
+--------+------------+-------+------+---------+
| 125 | dupond | 2016 | 1 | 125.00 |
| 125 | dupond | 2016 | 2 | 125.00 |
| 125 | dupond | 2016 | 3 | 125.00 |
| 125 | dupond | 2016 | 4 | 125.00 |
| 125 | dupond | 2016 | 5 | 125.00 |
| 125 | dupond | 2016 | 6 | 125.00 |
| 125 | dupond | 2016 | 7 | 125.00 |
| 125 | dupond | 2016 | 8 | 125.00 |
| 125 | dupond | 2016 | 9 | 125.00 |
| 125 | dupond | 2016 | 10 | 125.00 |
| 125 | dupond | 2016 | 11 | 125.00 |
| 125 | dupond | 2016 | 12 | 125.00 |
| 217 | durand | 2016 | 1 | 33.00 |
| 217 | durand | 2016 | 2 | 33.00 |
| 217 | durand | 2016 | 3 | 33.00 |
| 217 | durand | 2016 | 4 | 33.00 |
| 217 | durand | 2016 | 5 | 33.00 |
| 217 | durand | 2016 | 6 | 33.00 |
| 217 | durand | 2016 | 7 | 33.00 |
| 217 | durand | 2016 | 8 | 33.00 |
| 217 | durand | 2016 | 9 | 33.00 |
| 217 | durand | 2016 | 10 | 33.00 |
| 217 | durand | 2016 | 11 | 33.00 |
| 217 | durand | 2016 | 12 | 33.00 |
+--------+------------+-------+------+---------+
--------------
DROP TABLE IF EXISTS `sample`
--------------
--------------
CREATE TABLE `sample` as
select client,
annee,
'a' as periode,
sum(montant) as montant
from mystat
group by client, annee, periode
union
select client,
annee,
concat('t', quarter(concat(annee, '-', mois, '-01'))) as periode,
sum(montant) as montant
from mystat
group by client, annee, periode
union
select client,
annee,
concat('m', mois) as periode,
sum(montant) as montant
from mystat
group by client, annee, periode
order by client, annee, periode
--------------
--------------
select * from sample
--------------
+--------+-------+---------+---------+
| client | annee | periode | montant |
+--------+-------+---------+---------+
| 125 | 2016 | a | 1500.00 |
| 125 | 2016 | m1 | 125.00 |
| 125 | 2016 | m10 | 125.00 |
| 125 | 2016 | m11 | 125.00 |
| 125 | 2016 | m12 | 125.00 |
| 125 | 2016 | m2 | 125.00 |
| 125 | 2016 | m3 | 125.00 |
| 125 | 2016 | m4 | 125.00 |
| 125 | 2016 | m5 | 125.00 |
| 125 | 2016 | m6 | 125.00 |
| 125 | 2016 | m7 | 125.00 |
| 125 | 2016 | m8 | 125.00 |
| 125 | 2016 | m9 | 125.00 |
| 125 | 2016 | t1 | 375.00 |
| 125 | 2016 | t2 | 375.00 |
| 125 | 2016 | t3 | 375.00 |
| 125 | 2016 | t4 | 375.00 |
| 217 | 2016 | a | 396.00 |
| 217 | 2016 | m1 | 33.00 |
| 217 | 2016 | m10 | 33.00 |
| 217 | 2016 | m11 | 33.00 |
| 217 | 2016 | m12 | 33.00 |
| 217 | 2016 | m2 | 33.00 |
| 217 | 2016 | m3 | 33.00 |
| 217 | 2016 | m4 | 33.00 |
| 217 | 2016 | m5 | 33.00 |
| 217 | 2016 | m6 | 33.00 |
| 217 | 2016 | m7 | 33.00 |
| 217 | 2016 | m8 | 33.00 |
| 217 | 2016 | m9 | 33.00 |
| 217 | 2016 | t1 | 99.00 |
| 217 | 2016 | t2 | 99.00 |
| 217 | 2016 | t3 | 99.00 |
| 217 | 2016 | t4 | 99.00 |
+--------+-------+---------+---------+
--------------
select client, annee, max(an) as an,
max(trim_1) as trim_1, max(trim_2) as trim_2, max(trim_3) as trim_3, max(trim_4) as trim_4,
max(mois_01) as mois_01, max(mois_02) as mois_02, max(mois_03) as mois_03, max(mois_04) as mois_04,
max(mois_05) as mois_05, max(mois_06) as mois_06, max(mois_07) as mois_07, max(mois_08) as mois_08,
max(mois_09) as mois_09, max(mois_10) as mois_10, max(mois_11) as mois_11, max(mois_12) as mois_12
from (
select client,
annee,
case when periode='a' then montant else 0 end as an,
case when periode='t1' then montant else 0 end as trim_1,
case when periode='t2' then montant else 0 end as trim_2,
case when periode='t3' then montant else 0 end as trim_3,
case when periode='t4' then montant else 0 end as trim_4,
case when periode='m1' then montant else 0 end as mois_01,
case when periode='m2' then montant else 0 end as mois_02,
case when periode='m3' then montant else 0 end as mois_03,
case when periode='m4' then montant else 0 end as mois_04,
case when periode='m5' then montant else 0 end as mois_05,
case when periode='m6' then montant else 0 end as mois_06,
case when periode='m7' then montant else 0 end as mois_07,
case when periode='m8' then montant else 0 end as mois_08,
case when periode='m9' then montant else 0 end as mois_09,
case when periode='m10' then montant else 0 end as mois_10,
case when periode='m11' then montant else 0 end as mois_11,
case when periode='m12' then montant else 0 end as mois_12
from sample
) as x
group by client, annee
order by client, annee
--------------
+--------+-------+---------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| client | annee | an | trim_1 | trim_2 | trim_3 | trim_4 | mois_01 | mois_02 | mois_03 | mois_04 | mois_05 | mois_06 | mois_07 | mois_08 | mois_09 | mois_10 | mois_11 | mois_12 |
+--------+-------+---------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| 125 | 2016 | 1500.00 | 375.00 | 375.00 | 375.00 | 375.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 | 125.00 |
| 217 | 2016 | 396.00 | 99.00 | 99.00 | 99.00 | 99.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 | 33.00 |
+--------+-------+---------+--------+--------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager