日期:2014-05-17  浏览次数:20773 次

ORACLE怎样返回所有分组
各位请教一下,我目前在开发中遇到一个问题:
select 商品编号,sum(商品数量) as 总量 from table where 所在仓库='1' group by 商品编号
如我有一张表里面有商品编号,所在仓库,数量.....如下
商品编号 数量 所在仓库
a 1  1
a 2  1  
b 1  2
我执行这条语句后希望返回所有的商品数目也就是这样

商品编号 数量 所在仓库
a 3 1
b 0 1

我尝试过group by all 可是oracle好像不支持,请问各位我应该怎么写这个SQL语句?

------解决方案--------------------
SQL code

--我的是繫體,你改下就好了
select 商品編號,
    sum(decode(所在倉庫,'1',商品數量,0)) as 總量
from t
group by 商品編號,所在倉庫;

------解决方案--------------------
create table testTab
(
id varchar(10),
num number,
ran number
)

insert into testTab (id,num,ran) values('a',1,1);
insert into testTab (id,num,ran) values('a',2,1);
insert into testTab (id,num,ran) values('b',2,2);
insert into testTab (id,num,ran) values('a',2,2);
  
  
select id,
sum(decode(ran,ran,num)) as 总量,
ran 所在仓库
from testTab 
group by id,ran