2010-04-22 64 views
0

我需要一些幫助來制定select語句。我需要選擇以不同顏色爲每個部件發貨的總量。所以結果應該與顏色名稱和總數一致。將多個聚合函數作爲行返回

這裏是我的架構:

create table s 
    (sno char(5) not null, 
    sname char(20) not null, 
    status smallint, 
    city char(15), 
    primary key (sno) 
); 
create table p 
    (pno char(6) not null, 
    pname char(20) not null, 
    color char(6), 
    weight smallint, 
    city char(15), 
    primary key (pno) 
); 
create table sp 
    (sno char(5) not null, 
    pno char(6) not null, 
    qty integer not null, 
    primary key (sno, pno) 
); 
+0

此外,我不認爲UNIONs是允許的這個例子 – SDLFunTimes 2010-04-22 06:51:09

回答

0

當你的架構爲各自的產品PNO只有一個顏色,所以我不知道你的問題實際上需要。

銷售的產品:

select p.pno 
     , sum (sp.qty) 
from p join sp on (p.pno = sp.pno) 
group by p.pno; 

銷售顏色:

select p.color 
     , sum (sp.qty) 
from p join sp on (p.pno = sp.pno) 
group by p.color; 

編輯

爲了獲得銷售特定顏色很簡單:

select sum (sp.qty) 
from p join sp on (p.pno = sp.pno) 
where p.color = 'BLUE'; 
+0

我的意思是所有的貨物在SP總和那些對應於相同的顏色,並返回每個加總數量。 – SDLFunTimes 2010-04-22 07:13:47