日期:2014-05-18 浏览次数:20641 次
select a.corp_id, a.corp_name, b.salemoney, c.saleoutmoney, b.salemoney-c.saleoutmoney from x_kcorp a join (select sale_corp,sum(sale_money) as salemoney from x_sale group by sale_corp) b on a.corp_id=b.sale_corp join (select saleout_corp,sum(saleout_money) as saleoutmoney from x_saleout group by saleout_corp) c on a.a.corp_id=c.saleout_corp
------解决方案--------------------
use CSDN go --#1.直接减就可以 select a.corp_id, a.corp_name, (select sum(b.sale_money) from x_sale where sale_corp=a.corp_id) as salemoney, (select sum(b.saleout_money) from x_saleout where saleout_corp=a.corp_id) as saleoutmoney, diff = (select sum(b.sale_money) from x_sale where sale_corp=a.corp_id) - (select sum(b.saleout_money) from x_saleout where saleout_corp=a.corp_id) from x_kcorp a --#2.或者用子查询 select *, diff = salemoney - saleoutmoney from ( select a.corp_id, a.corp_name, (select sum(b.sale_money) from x_sale where sale_corp=a.corp_id) as salemoney, (select sum(b.saleout_money) from x_saleout where saleout_corp=a.corp_id) as saleoutmoney from x_kcorp a ) T