2016-12-09 71 views
-1
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)

+0

我会建议:改变你的数据模型。 –

+0

圣洁的废话伙计。也许保存临时表? – Rogue

+0

我的表格是从excel文档填充的 –

回答

2

首先,我会将Date_Effet添加到实际的索引中。 (索引中的第一个字段,因为您真的按日期过滤,并按Matricule, Nom, Section, Nom Section进行分组。)

然后,我会通过SUM(CASE(更改子查询并添加一个涵盖所有数据范围的WHERE子句。

select 
    Matricule, Nom, Section, Nom Section, 
    sum(case Presence_theorique_minutes when (Date_Effet BETWEEN '2016-1-1' and '2016-1-20') then Presence_theorique_minutes else 0 end) as PTM01, 
    sum(case Presence_theorique_minutes when (Date_Effet BETWEEN '2016-1-21' and '2016-2-20') then Presence_theorique_minutes else 0 end) as PTM02, 
    ... 
    sum(case Valeur_minutes when (Date_Effet BETWEEN '2016-1-1' and '2016-1-20') then Valeur_minutes else 0 end) as VM01, 
    sum(case Valeur_minutes when (Date_Effet BETWEEN '2016-1-21' and '2016-2-20') then Valeur_minutes else 0 end) as VM02, 
    ... 
    sum(case Presence_Badge_minutes when (Date_Effet BETWEEN '2016-1-1' and '2016-1-20') then Presence_Badge_minutes else 0 end) as PBM01, 
    sum(case Presence_Badge_minutes when (Date_Effet BETWEEN '2016-1-21' and '2016-2-20') then Presence_Badge_minutes else 0 end) as PBM02, 
from 
    Presence use index(Date_Effet, Matricule, Nom, Section, Nom Section) 
where 
    Date_Effet BETWEEN 'MIN_DATE' and 'MAX_DATE' 
group by 
    Matricule, Nom, Section, Nom Section; 

在这种情况下,由于日期范围(20到20),我会添加一个新的字段到表中。类似于analysis_period。建立这个字段为:YEAR + TEORIC月]

+------------+-----------------+ 
| date | analysis_period | 
+------------+-----------------+ 
| 2016-01-11 |  201601  | 
+------------+-----------------+ 
| 2016-01-21 |  201602  | 
+------------+-----------------+ 
| 2016-02-01 |  201602  | 
+------------+-----------------+ 
| 2016-02-25 |  201603  | 
+------------+-----------------+ 

然后,可以通过本领域容易组使用单SELECT

+0

105 IDOULAHIAN JAMAL 9180 ZSAC MECA 0 0 0 0 0 0 这一行它在每个字段中只返回0,我不知道为什么即使我的数据库在这个日期之间也不是空的 –

+0

你知道它应该使用哪个分区吗? – McNets

+0

你可以通过'从存在PARTITION partition_name'来改变'来自 存在使用指数'(Date_Effet,Matricule,Nom,Section,Nom Section)' – McNets