select a.ywdm, a.ywmc, a.sumsfbz, b.cnt, (a.sumsfbz * b.cnt) mul from (select yc.ywdm, yc.ywmc, sum(sm.sfbz) sumsfbz from t_s_ywmc yc, t_s_ywsf yf, t_s_sfxm sm where yc.ywdm = yf.ywdm and yf.xmid = sm.xmid and yc.ywdm in (select ywdm from t_s_invoiceprint t inner join t_s_invoice i on t.lsh = i.lsh where t.userid = ? and t.dates between to_date(?, 'yyyy-mm-dd hh24:mi') and to_date(?, 'yyyy-mm-dd hh24:mi') and repeal = 0 and relay = 0 group by i.ywdm) group by yc.ywdm, yc.ywmc) a, (select ywdm, count(ywdm) cnt from t_s_invoiceprint t inner join t_s_invoice i on t.lsh = i.lsh where t.userid = ? and t.dates between to_date(?, 'yyyy-mm-dd hh24:mi') and to_date(?, 'yyyy-mm-dd hh24:mi') and repeal = 0 and relay = 0 group by i.ywdm) b where a.ywdm = b.ywdm
from (select yc.ywdm, yc.ywmc, sum(sm.sfbz) sumsfbz
from t_s_ywmc yc, t_s_ywsf yf, t_s_sfxm sm
where yc.ywdm = yf.ywdm
and yf.xmid = sm.xmid
and yc.ywdm in
(select ywdm
from t_s_invoiceprint t
inner join t_s_invoice i on t.lsh = i.lsh
where t.userid = ?
and t.dates between to_date(?, 'yyyy-mm-dd hh24:mi') and
to_date(?, 'yyyy-mm-dd hh24:mi')
and repeal = 0
and relay = 0
group by i.ywdm) group by yc.ywdm, yc.ywmc) a,
(select ywdm, count(ywdm) cnt
from t_s_invoiceprint t
inner join t_s_invoice i on t.lsh = i.lsh
where t.userid = ?
and t.dates between to_date(?, 'yyyy-mm-dd hh24:mi') and
to_date(?, 'yyyy-mm-dd hh24:mi')
and repeal = 0
and relay = 0
group by i.ywdm) b
where a.ywdm = b.ywdm
外层的where条件有限制a.ywdm = b.ywdm
哇,果然是高人!我照你说的做了,确实去掉红色的部分以后达到一样的效果。但是如果没有外层的where条件有限制a.ywdm = b.ywdm。 那么该怎么弄。我还是想弄清sql语句中是否有类似代码重用这样的事情,大概应该如何做到?