SELECT Matricule, Nom, Section, Nom_Section,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pb1,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pt1,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv1,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-21' and '2016-2-20') and (t.Matricule=p.Matricule))) as pb2,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-21' and '2016-2-20') and (t.Matricule=p.Matricule))) as pt2,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-21' and '2016-2-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv2,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-2-21' and '2016-3-20') and (t.Matricule=p.Matricule))) as pb3,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-2-21' and '2016-3-20') and (t.Matricule=p.Matricule))) as pt3,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-2-21' and '2016-3-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv3,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-3-21' and '2016-4-20') and (t.Matricule=p.Matricule))) as pb4,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-3-21' and '2016-4-20') and (t.Matricule=p.Matricule))) as pt4,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-3-21' and '2016-4-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv4,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-4-21' and '2016-5-20') and (t.Matricule=p.Matricule))) as pb5,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-4-21' and '2016-5-20') and (t.Matricule=p.Matricule))) as pt5,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-4-21' and '2016-5-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv5,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-5-21' and '2016-6-20') and (t.Matricule=p.Matricule))) as pb6,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-5-21' and '2016-6-20') and (t.Matricule=p.Matricule))) as pt6,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-5-21' and '2016-6-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv6,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-6-21' and '2016-7-20') and (t.Matricule=p.Matricule))) as pb7,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-6-21' and '2016-7-20') and (t.Matricule=p.Matricule))) as pt7,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-6-21' and '2016-7-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv7,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-7-21' and '2016-8-20') and (t.Matricule=p.Matricule))) as pb8,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-7-21' and '2016-8-20') and (t.Matricule=p.Matricule))) as pt8,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-7-21' and '2016-8-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv8,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pb9,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pt9,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-5-21' and '2016-6-3') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv9,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pb10,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pt10,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv10,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pb11,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pt11,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv11,
(select sum(Presence_Badge_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pb12,
(select sum(Presence_theorique_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (t.Matricule=p.Matricule))) as pt12,
(select sum(Valeur_minutes) from presence t where ((Date_Effet BETWEEN '2016-1-1' and '2016-1-20') and (Motif in ('ABSI','CP','CMAR','CMAT','CNAI','COCH','FERI','FINP','DEPL','CODC','DELE','FORM','HSUP','MALA','VMED','ALET','RECU','ACTBAD'))and (t.Matricule=p.Matricule))) as pv12
from Presence p group by Matricule;
。我已解释查询 的图片在MySQL这个SQL查询,这是我的表enter image description here如何优化
CREATE TABLE `presence` (`id` int(10) NOT NULL AUTO_INCREMENT,
`date_effet` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`matricule` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`motif` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`nom` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`nom_section` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT
NULL,
`presence_badge` tinyblob,
`presence_badge_minutes` int(10) NOT NULL,
`presence_theorique` tinyblob,
`presence_theorique_minutes` int(10) NOT NULL,
`section` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`valeur` tinyblob,
`valeur_minutes` int(10) NOT NULL,
PRIMARY KEY (`id`,`date_effet`),
KEY `matricule_index` (`matricule`),
KEY `pres_index` (`matricule`,`nom`,`section`,`nom_section`)
) ENGINE=InnoDB AUTO_INCREMENT=673810 DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (TO_DAYS(date_effet))
(PARTITION p_jan VALUES LESS THAN (736349) ENGINE = InnoDB,
PARTITION p_fev VALUES LESS THAN (736380) ENGINE = InnoDB,
PARTITION p_mar VALUES LESS THAN (736409) ENGINE = InnoDB,
PARTITION p_avr VALUES LESS THAN (736440) ENGINE = InnoDB,
PARTITION p_mai VALUES LESS THAN (736470) ENGINE = InnoDB,
PARTITION p_jui VALUES LESS THAN (736501) ENGINE = InnoDB,
PARTITION p_juil VALUES LESS THAN (736531) ENGINE = InnoDB,
PARTITION p_aou VALUES LESS THAN (736562) ENGINE = InnoDB,
PARTITION p_sep VALUES LESS THAN (736593) ENGINE = InnoDB,
PARTITION p_oct VALUES LESS THAN (736623) ENGINE = InnoDB,
PARTITION p_nov VALUES LESS THAN (736654) ENGINE = InnoDB,
PARTITION p_dec VALUES LESS THAN (736684) ENGINE = InnoDB) */
我没有创建列“date_effet”和两个指数一个分区范围(matricule,Nom,Section,Nom_section)
我会建议:改变你的数据模型。 –
圣洁的废话伙计。也许保存临时表? – Rogue
我的表格是从excel文档填充的 –