62,046
社区成员
发帖
与我相关
我的任务
分享
declare @t table( it_id int , it_qty int )
insert into @t values( 1 , 50 )
insert into @t values( 2 , 70 )
declare @t1 table( ot_id int , ot_qty int , it_id int )
insert into @t1 values(001 , 40 , 1 )
insert into @t1 values(002 , 20 , 2 )
select a.it_id , min(a.it_qty) -SUM(b.ot_qty) as qty from @t a
inner join @t1 b on b.it_id = a.it_id
group by a.it_id
having( min(a.it_qty) -SUM(b.ot_qty) >0)
select a.it_id,a.it_qty-b.ot_qty,b.ot_id
from a inner join b on a.it_id=b.it_id order by a.it_id;
it_id int,
it_qty int
)
insert into #A select 1,50
insert into #A select 2,70
create table #B
(
ot_id nchar(10),
ot_qty int,
it_id int
)
insert into #B select '001',40,1
insert into #B select '002',20,2
select b.it_id,(A.it_qty-B.ot_qty) '剩余的'
from #A A join #B B
on A.it_id=b.it_id
it_id 剩余的
----------- -----------
1 10
2 50
select a.it_id,mim(a.it_qty)-SUM(b.ot_qty) as '剩余的' from a inner join b
on a.it_id =b.it_id
group by a.it_id
having (MIN(a.it_qty)-SUM(b.ot_qty)>0)