有一个商品货位表
他由一个主从表结构得到的数据。
货品基础资料表
货品编码, 货品名称, 库存上限 , 库存下限
001 酒 2 -10
002 烟 10 3
主表
仓库编号,仓库名称
1 仓库1
2 仓库2
从表
仓库编号(外键) 货品编码, 数量
1 001 3
1 002 5
2 001 6
2 001 1
得到如下的库存报警明细表。
组合框选项,全部仓库、仓库1、仓库2、仓库3
当选择全部仓库
得到下面这个样式.
货品编码, 货品名称, 仓库名称, 库存下限,当前库存,库存上限
001 酒 null -10 10 2
null null 仓库1 null 3 null
null null 仓库2 null 7 null
小计 null null null 10 null
null null null null null null
002 烟 null 3 5 10
null null 仓库1 null 5 null
null null 仓库2 null 0 null
小计 null null null 5 null
null null null null null null
大超难度的
分全用完了。本应加上200分。
---------------------------------------------------------------
更正:
select case
when ordertype=1 then 货品编码
when ordertype=3 then '小计'
else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限
from
(
select
a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype
from 货品基础资料表 a left join
(
select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码
) as b on a.货品编码=b.货品编码
union all
select
c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码,d.仓库名称
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
) as x
order by rtrim(货品编码),ordertype