27,579
社区成员
发帖
与我相关
我的任务
分享
create table t (东西 varchar(30) , 位置 varchar(30) , 数量 int )
insert into t values('物品A', '位置1', 1000)
insert into t values('物品A', '位置2', 2000)
insert into t values('物品B', '位置1', 4000)
insert into t values('物品B', '位置3', 1000)
insert into t values('物品C', '位置2', 1500)
go
select GETDATE() ,
SUM( case when 位置 = '位置1' then 数量 else 0 end) 位置1 ,
SUM( case when 位置 = '位置2' then 数量 else 0 end) 位置2 ,
SUM( case when 位置 = '位置3' then 数量 else 0 end) 位置3
from t
select GETDATE() ,
SUM( case when 东西 = '物品A' then 数量 else 0 end) 物品A ,
SUM( case when 东西 = '物品B' then 数量 else 0 end) 物品B ,
SUM( case when 东西 = '物品C' then 数量 else 0 end) 物品C
from t
go
drop table t
位置1 位置2 位置3
----------------------- ----------- ----------- -----------
2014-12-20 10:42:56.280 5000 3500 1000
(1 行受影响)
物品A 物品B 物品C
----------------------- ----------- ----------- -----------
2014-12-20 10:42:56.280 3000 5000 1500
(1 行受影响)
select GETDATE() 日期,SUM(数量) 数量 ,位置 from t group by 位置
日期 数量 位置
2014-11-26 13:46:45.497 5000 位置1
2014-11-26 13:46:45.497 3500 位置2
2014-11-26 13:46:45.497 1000 位置3