行列转换
https://blog.csdn.net/mchdba/article/details/39163695
第一步,求两个表各自的数据
select DATE(a.create_date) as 'nianfen',
sum(IF(a.type="0",1,0)) AS "shanghu",
SUM(IF(a.type="1",1,0)) AS "banzu",
SUM(IF(a.type="2",1,0)) AS "company",
0 as 'zhaogong',
0 as 'zhaohuo',
0 as 'gongqiu',
0 as 'gongcheng',
0 as 'jianshe',
0 as 'tujian',
0 as 'advert',
0 as 'dianqi',
0 as 'yuanlin'
from
dp_info a where DATE_FORMAT(create_date,'%Y-%m')='2018-05'
group by DATE(a.create_date)
union all
select DATE(a.date) as 'nianfen',
0 as 'shanghu',
0 as 'banzu',
0 as 'company',
sum(IF(a.to_type="招工信息",1,0)) AS "zhaogong",
SUM(IF(a.to_type="找活信息",1,0)) AS "zhaohuo",
SUM(IF(a.to_type="供求信息",1,0)) AS "gongqiu",
SUM(IF(a.to_type="工程机械",1,0)) AS "gongcheng",
SUM(IF(a.to_type="建设服务",1,0)) AS "jianshe",
SUM(IF(a.to_type="土建材料",1,0)) AS "tujian",
SUM(IF(a.to_type="装修材料",1,0)) AS "advert",
SUM(IF(a.to_type="电气材料",1,0)) AS "dianqi",
SUM(IF(a.to_type="园林景观",1,0)) AS "yuanlin"
from
fb_fabu a where DATE_FORMAT(date,'%Y-%m')='2018-05'
group by DATE(a.date)
第二步,求两个表合在一起的数据
select
xx.nianfen as 'nianfen',
sum(xx.zhaogong) as "zhaogong",
sum(xx.zhaohuo) as 'zhaohuo',
sum(xx.gongqiu) as 'gongqiu',
sum(xx.gongcheng) as 'gongcheng',
sum(xx.jianshe) as 'jianshe',
sum(xx.shanghu) as 'shanghu',
sum(xx.company) as 'company',
sum(xx.banzu) as 'banzu',
SUM(xx.tujian) AS "tujian",
SUM(xx.advert) AS "advert",
SUM(xx.dianqi) AS "dianqi",
SUM(xx.yuanlin) AS "yuanlin",
SUM(xx.tujian) AS "tujian"
SUM(IF(a.to_type="园林景观",1,0)) AS "yuanlin"
from
(
第一步内容
) xx
GROUP BY xx.nianfen
第三步,求和
select
'总计' as 'nianfen',
sum(ddd.zhaogong) as "zhaogong",
sum(ddd.zhaohuo) as 'zhaohuo',
sum(ddd.gongqiu) as 'gongqiu',
sum(ddd.gongcheng) as 'gongcheng',
sum(ddd.jianshe) as 'jianshe',
sum(ddd.shanghu) as 'shanghu',
sum(ddd.company) as 'company',
sum(ddd.banzu) as 'banzu',
SUM(ddd.tujian) AS "tujian",
SUM(ddd.advert) AS "advert",
SUM(ddd.dianqi) AS "dianqi",
SUM(ddd.yuanlin) AS "yuanlin"
from
(
第二步的内容
)ddd
第四部合并