1. 程式人生 > >行列轉換

行列轉換

sha create 轉換 com creat group by sum lec tps

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

第四部合並

行列轉換