SQL查询问题!如下表:我想查询出某物料批次入库减去批次出库大于0的所有批次!该怎么操作?

2025-04-07 06:24:23
推荐回答(2个)
回答1:

select 物料代号,入库数量,出库数量 from
(select 物料代号,sum(出入库数量) as 入库数量 from  S_ckchuriBOM where 操作类别 = '入库'
group by 物料代号) tIn,
(select 物料代号,sum(出入库数量) as 出库数量 from  S_ckchuriBOM where 操作类别 = '出库'
group by 物料代号) tOut
where tIn.物料代号=tOut.物料代号 and 入库数量>出库数量
  --你试试

回答2:

看看这个是否可以,比较粗糙:
select DISTINCT 操作批次
from S_ckchuriBOM
where id in(
select id,SUM( 出库数量)
from(
select id,
出库数量=cse 操作类别 when '入库' then convert(int,出库数量) else -convert(int,出库数量) end
from S_ckchuriBOM
) A
where SUM( 出库数量) > 0
)