- mysql--------
- select a.dname,
- max(if(b.mon='一月份',b.yj,0)) 一月份,
- max(if(b.mon='二月份',b.yj,0)) 二月份,
- max(if(b.mon='三月份',b.yj,0)) 三月份
- from t2 a left join t1 b on a.dep=b.dep
- group by dname
- mysql-------
- select a.dname,
- sum(case when b.mon='一月份' then b.yj else 0 end) 一月份,
- sum(case when b.mon='二月份' then b.yj else 0 end) 二月份,
- sum(case when b.mon='三月份' then b.yj else 0 end) 三月份
- from t2 a left join t1 b on a.dep=b.dep
- group by dname
- oracle------注:decode的方法与mysql的不同
- select a.dname,
- max(decode(b.mon,'一月份',b.yj,0) 一月份,
- max(decode(b.mon,'二月份',b.yj,0) 二月份,
- max(decode(b.mon,'三月份',b.yj,0) 三月份)
- from table2 a left join table1 b on a.dep=b.dep
- group by dname