decode其实就是if-else,如:decode(ybc,null,0,ybc),,这个意味着如果ybc列==null,则函数返回0,否则返回ybc的值。
再如,decode(变量,条件1,值1,条件2,值2),这个就意味着是:if-elsif,如果是decode(变量,条件1,值1,条件2,值2,值3),这个则是if-elsif-else。
oracle的行转列的具体参考网址:http://www.2cto.com/database/201108/100792.html
下面是的代码:
insert into tj_kszgjf(id,bjdm,xxdm,xqdm,dsdm,ybc,xxtj,xqtj,dstj,tj_kszgjf.zstg,tj_kszgjf.sum, shijian,flag)
select seq_kszgjftj.nextval,bj.bjdm,bj.xx_xxdm as xxdm,xx.xq_xqdm as xqdm,xq.ss as dqdm,
decode(ybc,null,0,ybc),
decode(xxtj,null,0,xxtj),
decode(xqtj,null,0,xqtj),
decode(dstj,null,0,dstj),
decode(zstg,null,0,zstg),
decode("sum",null,0,"sum"),
sysdate,flag
from
(select zgjf.bjdm,
sum(decode(zgjf.zt, 1,1,0)) ybc,
sum(decode(zgjf.zt, 2, 1,0)) xxtj,
sum(decode(zgjf.zt, 3, 1,0)) xqtj,
sum(decode(zgjf.zt, 4, 1,0)) dstj,
sum(decode(zgjf.zt, 5, 1,0)) zstg,
count(bmxh)as "sum",
flag
from zgjf
group by flag,bjdm
)zgjftemp
right join bj on zgjftemp.bjdm=bj.bjdm
inner join xx on bj.xx_xxdm=xx.zxdm
inner join xq on xx.xq_xqdm=xq.xqdm;
commit;