如何查询汇总交叉表

时间:2014-08-26 02:36:17

标签: php mysql phpmyadmin

我试图在mysql中使用交叉表,我可以按月和行进行汇总。 但我想按月做总结。知道如何修改我的查询吗?

我的表: 表1

jobid    jobtype    jobcomplete
14001      1        2014-01-05
14002      1        2014-01-07
14003      1        2014-02-01
14004      2        2014-01-05
14005      3        2014-02-08
14006      1        2014-03-05
14007      1        2014-04-05
14008      1        2014-05-05
14009      1        2014-06-05
14010      1        2014-07-05
14011      1        2014-08-05
14012      1        2014-09-05
14013      1        2014-10-05
14014      1        2014-11-05
14015      1        2014-12-05
14016      2        2014-12-05  

表2

jobtype    desc
  1        hardware
  2        software
  3        network

使用交叉表:

Select t2.desc as test,sum(IF(Month(t1.jobcomplete)=1,1,0)) AS JAN,
sum(IF(Month(t1.jobcomplete)=2,1,0)) AS FAB,
sum(IF(Month(t1.jobcomplete)=3,1,0)) AS MAR,
sum(IF(Month(t1.jobcomplete)=4,1,0)) AS APR,
sum(IF(Month(t1.jobcomplete)=5,1,0)) AS MAY,
sum(IF(Month(t1.jobcomplete)=6,1,0)) AS JUN,
sum(IF(Month(t1.jobcomplete)=7,1,0)) AS JUL,
sum(IF(Month(t1.jobcomplete)=8,1,0)) AS AUG,
sum(IF(Month(t1.jobcomplete)=9,1,0)) AS SEP,
sum(IF(Month(t1.jobcomplete)=10,1,0)) AS OCT,
sum(IF(Month(t1.jobcomplete)=11,1,0)) AS NOV,
sum(IF(Month(t1.jobcomplete)=12,1,0)) AS DEC,
Count(t1.job_complete) as Total
FROM table1 as t1 INNER JOIN table2 as t2 ON t1.jobtype=t2.jobtype GROUP BY t1.jobtype

这给了我这个:

   test      JAN  FAB  MAR  APR  MAY  JUN  JUL  AUG  SEP OCT  NOV  DEC  Total  
 hardware     2    1    1    1    1    1    1    1    1   1    1    1    13
 software     1    0    0    0    0    0    0    0    0   0    0    1     2
 network      0    1    0    0    0    0    0    0    0   0    0    0     1

我想要这个结果:

    test      JAN  FAB  MAR  APR  MAY  JUN  JUL  AUG  SEP OCT  NOV  DEC  Total  
   hardware     2    1    1    1    1    1    1    1    1   1    1    1    13
   software     1    0    0    0    0    0    0    0    0   0    0    1     2
   network      0    1    0    0    0    0    0    0    0   0    0    0     1
   TOTAL        2    2    1    1    1    1    1    1    1   1    1    2    16

非常感谢,抱歉我的英语不好。我不擅长英语。

1 个答案:

答案 0 :(得分:1)

我能做到。感谢@Sean的回答。

(Select t2.desc as test,sum(IF(Month(t1.jobcomplete)=1,1,0)) AS JAN,
sum(IF(Month(t1.jobcomplete)=2,1,0)) AS FAB,
sum(IF(Month(t1.jobcomplete)=3,1,0)) AS MAR,
sum(IF(Month(t1.jobcomplete)=4,1,0)) AS APR,
sum(IF(Month(t1.jobcomplete)=5,1,0)) AS MAY,
sum(IF(Month(t1.jobcomplete)=6,1,0)) AS JUN,
sum(IF(Month(t1.jobcomplete)=7,1,0)) AS JUL,
sum(IF(Month(t1.jobcomplete)=8,1,0)) AS AUG,
sum(IF(Month(t1.jobcomplete)=9,1,0)) AS SEP,
sum(IF(Month(t1.jobcomplete)=10,1,0)) AS OCT,
sum(IF(Month(t1.jobcomplete)=11,1,0)) AS NOV,
sum(IF(Month(t1.jobcomplete)=12,1,0)) AS DEC,
Count(t1.job_complete) as Total
FROM table1 as t1 INNER JOIN table2 as t2 ON t1.jobtype=t2.jobtype GROUP BY t1.jobtype)
UNION
(Select 'TOTAL' As TOTAL,sum(IF(Month(t1.jobcomplete)=1,1,0)) AS JAN,
sum(IF(Month(t1.jobcomplete)=2,1,0)) AS FAB,
sum(IF(Month(t1.jobcomplete)=3,1,0)) AS MAR,
sum(IF(Month(t1.jobcomplete)=4,1,0)) AS APR,
sum(IF(Month(t1.jobcomplete)=5,1,0)) AS MAY,
sum(IF(Month(t1.jobcomplete)=6,1,0)) AS JUN,
sum(IF(Month(t1.jobcomplete)=7,1,0)) AS JUL,
sum(IF(Month(t1.jobcomplete)=8,1,0)) AS AUG,
sum(IF(Month(t1.jobcomplete)=9,1,0)) AS SEP,
sum(IF(Month(t1.jobcomplete)=10,1,0)) AS OCT,
sum(IF(Month(t1.jobcomplete)=11,1,0)) AS NOV,
sum(IF(Month(t1.jobcomplete)=12,1,0)) AS DEC,
Count(t1.job_complete) as Total
FROM table1 as t1 )