这是原表:
想实现这种效果:
过程我就不写了,把原代码贴给大家:
1 select (select qhmc from tab_qh c where c.qhid=b.sjqhbh) sjqh ,qhmc,a.lx,'全县平均值' as fl, 2 max(case zbbh when '1' then Pjz else 0 end) as bh1, 3 max(case zbbh when '2' then Pjz else 0 end) as bh2, 4 max(case zbbh when '3' then Pjz else 0 end) as bh3, 5 max(case zbbh when '4' then Pjz else 0 end) as bh4, 6 max(case zbbh when '5' then Pjz else 0 end) as bh5, 7 max(case zbbh when '6' then Pjz else 0 end) as bh6, 8 max(case zbbh when '7' then Pjz else 0 end) as bh7, 9 max(case zbbh when '8' then Pjz else 0 end) as bh8, 10 max(case zbbh when '9' then Pjz else 0 end) as bh9 11 from tab_xjjhpg a inner join tab_qh b on a.qhid=b.qhid 12 where nf='2012' 13 group by b.sjqhbh,qhmc,a.lx 14 union 15 select (select qhmc from tab_qh c where c.qhid=b.sjqhbh) sjqh ,qhmc,a.lx,'差异系数' as fl, 16 max(case zbbh when '1' then cyxs else 0 end) as bh1, 17 max(case zbbh when '2' then cyxs else 0 end) as bh2, 18 max(case zbbh when '3' then cyxs else 0 end) as bh3, 19 max(case zbbh when '4' then cyxs else 0 end) as bh4, 20 max(case zbbh when '5' then cyxs else 0 end) as bh5, 21 max(case zbbh when '6' then cyxs else 0 end) as bh6, 22 max(case zbbh when '7' then cyxs else 0 end) as bh7, 23 max(case zbbh when '8' then cyxs else 0 end) as bh8, 24 max(case zbbh when '9' then cyxs else 0 end) as bh9 25 from tab_xjjhpg a inner join tab_qh b on a.qhid=b.qhid 26 where nf='2012' 27 group by b.sjqhbh,qhmc,a.lx 28 order by qhmc,sjqh
注意点:1、使用mask-case
2、union联合查询
3、添加一个字段,这里我添加的是'全县平均值'as fl 和'差异系数' as fl