有表a 结构如下 pid pnum sid
1 10 01
1 12 02
2 8 01
3 11 01
3 8 03
其中 pid 为产品编号 pnum为产品数量 sid为仓库id 请用编写sql 语句 实现将上表中的数据合并pid s1id s2id s3id1 10 12 0
2 8 0 0
3 11 0 8 s1id 为仓库01 的库存量
s2id 为仓库02 的库存量
s3id 为仓库03 的库存量
如果该产品在某仓库中无库存量 用0代替
1 10 01
1 12 02
2 8 01
3 11 01
3 8 03
其中 pid 为产品编号 pnum为产品数量 sid为仓库id 请用编写sql 语句 实现将上表中的数据合并pid s1id s2id s3id1 10 12 0
2 8 0 0
3 11 0 8 s1id 为仓库01 的库存量
s2id 为仓库02 的库存量
s3id 为仓库03 的库存量
如果该产品在某仓库中无库存量 用0代替
解决方案 »
- IE8 下 网页里面 new ActiveXObject 总是不成功?
- asp.net多网站(同一Domain下)同时登录问题
- asp.net的css样式在各个浏览器中显示的样式都不一样?怎么解决啊?
- DataTable或DataSet如何取前N行数据
- asp.net中如何根据用户传入的不定数参数,动态建立GRIDVIEW控件,并动态绑定查询到的数据并显示出来
- 大家看看在 Response.Write 里如何使用变量!
- 高手指教——Web库房管理不允许输入空值的提示问题。。。:-(
- !!!盼望各位老鸟指点指点,有分
- 请问如何知道一个文件夹的大小
- 写cookie报错误,请大家帮忙看看`~~
- asp.net 存储过程,我要返回记录集,和记录总数
- 哪位高手帮忙解决一个问题,给个思路也行
INSERT INTO a
SELECT '1','10','01' union all
SELECT '1','12','02' union all
SELECT '2','8','01' union all
SELECT '3','11','01' union all
SELECT '3','8','03' select pid,sum(case sid when '01' then pnum else 0 end) as s1id,
sum(case sid when '02' then pnum else 0 end) as s2id,
sum(case sid when '03' then pnum else 0 end) as s3id
from a group by pidDROP TABLE a
drop table tb1
Go
Create table tb1(pid int,pnum int,sid nvarchar(2))
Insert tb1
select 1,10,N'01' union all
select 1,12,N'02' union all
select 2,8,N'01' union all
select 3,11,N'01' union all
select 3,8,N'03'
Go
select
pid,
sum(case when [sid]='01' then pnum else 0 end) as s1id,
sum(case when [sid]='02' then pnum else 0 end) as s2id,
sum(case when [sid]='03' then pnum else 0 end) as s3id
from tb1
group by pid