22,209
社区成员
发帖
与我相关
我的任务
分享
select 商品编号,名称,规格,单位,
sum(case when 日期<='2017-12-31' and type='入库' then 数量
when 日期<='2017-12-31' and type='出库' then -1*数量
end ) as '去年结存',
sum(case when 日期>='2018-01-01' and type='入库' then 数量 else 0 end) as '今年入库',
sum(case when 日期>='2018-01-01' and type='出库' then 数量 else 0 end) as '今年出库',
sum(case when type='入库' then 数量
else -1*数量
end ) as '今年结存'
from
(select 商品编号,名称,规格,入库数量 as 数量,单位,入库日期,'入库' as type from 入库表
where 入库日期<='2018-08-25' and 入库仓库='X1仓库'
union all
select 商品编号,名称,规格,出库数量,单位,出库日期,'出库' from 出库表
where 出库日期<='2018-08-25' and 出库日期='X1仓库') as A
group by 商品编号,名称,规格,单位