三个表YSXS1,YSXS2,CTXS
需要向CTXS表插入ctid,time,yye,kds其中
ctid,time,为固定值,yye,kds都是查询出来的,
求问插入语句怎么写
两个子查询如下:
yye=
select sum(cpje) from YSXS1 where
ctid='N001'and time='201401',
kds=
select kdsl from YSXS2 where
ctid='N001'and time='201401';
------解决方案--------------------
insert into ctxs(ctid,time,yye,kds,kdj)
select '固定值' as ctid, ' 固定值' as time,(select sum(cpje) from YSXS1 where
ctid='N001'and time='201401') as yye,(select kdsl from YSXS2 where
ctid='N001'and time='201401) as kds,(select sum(cpje) from YSXS1 where
ctid='N001'and time='201401')/(select kdsl from YSXS2 where
ctid='N001'and time='201401) as kdj
如果得出的数据是整型,可能会导致截断小数点,如果是要保留小数点,可以用:
insert into ctxs(ctid,time,yye,kds,kdj)
select '固定值' as ctid, ' 固定值' as time,(select sum(cpje) from YSXS1 where
ctid='N001'and time='201401') as yye,(select kdsl from YSXS2 where
ctid='N001'and time='201401) as kds,(select sum(cpje) from YSXS1 where
ctid='N001'and time='201401')*1.0/(select kdsl from YSXS2 where
ctid='N001'and time='201401) as kdj