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 入库数量>出库数量
--你试试
看看这个是否可以,比较粗糙:
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
)