create table test(
商品类别 varchar(50),
商品名称 varchar(50),
商品规格 varchar(50),
商品单价 decimal(13,2),
数量 int,
出入库状态 char(1)--出入库状态(1为入库,0为出库)
)
insert into test select '服装','西服','175xx',1000.00,10,'1'--单价为1000的西服入库了10件
insert into test select '服装','西服','175xx',2000.00,20,'1'--单价为2000的西服入库了20件
insert into test select '服装','西服','175xx',1000.00,10,'0'--单价为1000的西服出库了10件
insert into test select '服装','西服','175xx',2000.00,5,'0'--单价为2000的西服出库了5件/*
select 商品类别,商品名称,商品规格,商品单价,
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价
*//*
--要求以商品类别,商品名称,商品规格,商品单价进行分组,得出库存结余数商品类别 商品名称 商品规格 商品单价 库存结余数量服装 西服 175xx 2000 15
*/
drop table test说明: 当单价1000的西服库存数为0的时候,不显示单价1000的西服的查询结果数据.
商品类别 varchar(50),
商品名称 varchar(50),
商品规格 varchar(50),
商品单价 decimal(13,2),
数量 int,
出入库状态 char(1)--出入库状态(1为入库,0为出库)
)
insert into test select '服装','西服','175xx',1000.00,10,'1'--单价为1000的西服入库了10件
insert into test select '服装','西服','175xx',2000.00,20,'1'--单价为2000的西服入库了20件
insert into test select '服装','西服','175xx',1000.00,10,'0'--单价为1000的西服出库了10件
insert into test select '服装','西服','175xx',2000.00,5,'0'--单价为2000的西服出库了5件/*
select 商品类别,商品名称,商品规格,商品单价,
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价
*//*
--要求以商品类别,商品名称,商品规格,商品单价进行分组,得出库存结余数商品类别 商品名称 商品规格 商品单价 库存结余数量服装 西服 175xx 2000 15
*/
drop table test说明: 当单价1000的西服库存数为0的时候,不显示单价1000的西服的查询结果数据.
from
(select 商品类别,商品名称,商品规格,商品单价,
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价) as a
where a.库存结余数量<>0
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价
having(sum(case 出入库状态 when '1' then 数量 else -数量 end))<>0
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价
having sum(case 出入库状态 when '1' then 数量 else -数量 end) <> 0
from
(select 商品类别,商品名称,商品规格,商品单价,
sum(case 出入库状态 when '1' then 数量 else -数量 end) as 库存结余数量
from test
group by 商品类别,商品名称,商品规格,商品单价) as a
where a.库存结余数量<>0
superman_yc(心有灵犀)方法可行的