27,579
社区成员
发帖
与我相关
我的任务
分享
select id=isnull(b.id,a.id),
qty=sum(a.qty)
from #A a
left join #B b on a.id=b.subid
group by isnull(b.id,a.id)
/*
id qty
----------- -----------
1 2
2 4
(2 rows affected)
*/
Select a.id,sum(a.qty) as qty
From #A a
Left Join #B b on a.id=b.id
Where a.id not in (Select subid From #B)
Group By a.id