select 0 as fflag,fcode,fprice,famount
from tablaname
union (select 1 as fflag,'合计:' as fcode,0 as fprice,sum(famount) as famount
from tablaname)
order by fflag,fcode
from tablaname
union (select 1 as fflag,'合计:' as fcode,0 as fprice,sum(famount) as famount
from tablaname)
order by fflag,fcode
(
select 0 as fflag,fcode,fprice,famount
from tablaname
union
select 1 as fflag,'合计:' as fcode,0 as fprice,sum(famount) as famount
from tablaname
) as a
order by fflag,fcode
你若再说能不能不用不着SQL语句,我想数据库开发版一大半的人会饿死的。开个玩笑,只是因为实不是有点想不通通 :)
h.rkje as 入库金额,h.cksl as 出库数量,h.ckje as 出库金额,h.tzsl as 调增数量,
h.tzje as 调增金额,h.tjsl as 调减数量,h.tjje as 调减金额,h.drsl as 调入数量,
h.drje as 调入金额,h.dcsl as 调出数量,h.dcje as 调出金额,i.qmsl as 期末库存,
i.qmje as 期末金额
from (select a.kfbm,sum(a.fssl*c.rk) as rksl,sum(a.fssl*a.dj*c.rk) as rkje,
sum(a.fssl*c.ck) as cksl,sum(a.fssl*a.dj*c.ck) as ckje,
sum(a.fssl*c.tz) as tzsl,sum(a.fssl*a.dj*c.tz) as tzje,
sum(a.fssl*c.tj) as tjsl,sum(a.fssl*a.dj*c.tj) as tjje,
sum(a.fssl*c.dr) as drsl,sum(a.fssl*a.dj*c.dr) as drje,
sum(a.fssl*c.dc) as dcsl,sum(a.fssl*a.dj*c.dc) as dcje
from crkmx a,tj c
where a.zy=c.zy and a.fsrq>='" & m_time1 & "' and
a.fsrq<='" & m_time2 & "'
group by a.kfbm) h,
(select m.kfbm,sum(m.qcsl) as qcsl,sum(m.qcje) as qcje,sum(m.qmsl) as qmsl,
sum(m.qmje) as qmje
from (select x.kfbm,x.pz,x.cd,x.gys,x.dj,x.maxdate,y.kcsl as qcsl,
y.kcsl*x.dj as qcje,x.kcsl as qmsl,x.dj*x.kcsl as qmje
from (select a.*
from crkmx a,(select max(ywxh) as ywxh,kfbm
from crkmx
where fsrq<='" & m_time2 & "'
group by kfbm,pz,cd,dj,gys,maxdate) b
where a.ywxh=b.ywxh and a.kfbm=b.kfbm) x
left join (select a.*
from crkmx a,(select max(ywxh) as ywxh,kfbm
from crkmx
where fsrq<'" & m_time1 & "'
group by kfbm,pz,cd,dj,gys,maxdate) b
where a.ywxh=b.ywxh and a.kfbm=b.kfbm) y
on x.kfbm=y.kfbm) m
group by m.kfbm)
i,ggbm j,pmbm k
where h.kfbm=i.kfbm and i.kfbm=j.kfbm and j.pmbm=k.pmbm
order by j.pmbm