不知道是不是这个意思!?create table A
(
   id int,
   Quantity int
)create table B
(
   id int,
   Foreignid int,
   Quantity int
)insert A select 1,10insert B select 1,1,2
insert B select 2,1,3
insert B select 3,1,1select Quantity from B where Foreignid in (select A.id from A,(select Foreignid,sum(Quantity) as Tq from B group by Foreignid) T where  A.id=T.Foreignid  and  A.Quantity>=T.Tq )
union all
select A.Quantity-T.Tq as Quantity from A,(select Foreignid,sum(Quantity) as Tq from B group by Foreignid) T where  A.id=T.Foreignid  and  A.Quantity>=T.Tq