17,377
社区成员
发帖
与我相关
我的任务
分享
1.本人觉得此效率最好
select 品名,sum(数量) 库存
from (select 品名,数量 数量 from A
union all
select 品名,-数量 数量 from B)
group by 品名
PLSQL 下测试:
with table1 as (
select 'A' as id, 990 as total from dual union all
select 'B' as id, 1200 as total from dual
),
table2 as
(
select 'A' as id, 99 as consume from dual union all
select 'A' as id, 89 as consume from dual union all
select 'B' as id, 77 as consume from dual union all
select 'B' as id, 109 as consume from dual
)
select a.id, total - consume as remain from
(
select id, sum(total) total from table1 group by id
) a
inner join
(
select id, sum(consume) consume from table2 group by id
) b on a.id = b.id
-----------
A 802
B 1014
with A as(
select 'A' a,990 b from dual
union all
select 'B' a,1200 b from dual
),B as(
select 'A' a,99 c from dual
union all
select 'A' a,89 c from dual
union all
select 'B' a,77 c from dual
union all
select 'B' a,109 c from dual
)
select a.a,b-sum(c) from a,b where a.a = b.a group by a.a,b
select name,sum(数量)
from (select name ,数量 from 表1
union all
select name ,-1*数量 from 表1
)
group by name