2013-03-25 56 views
0

我有一個看起來像微不足道的問題,但我無法使它工作。它在Oracle SQL中。 這裏是腳本的樣品:如何從類別和生產者組中選擇具有最低價格的產品?

create table product (
product_id number primary key, 
name varchar(255) 
); 

create table producer (
producer_id number primary key, 
name varchar(255) 
); 

create table catalog (
pp_product_id number, 
pp_producer_id number, 
price number 
); 


alter table catalog add constraint pp_product_id1 foreign key (pp_product_id) references product (product_id); 
alter table catalog add constraint pp_product_id2 foreign key (pp_producer_id) references producer (producer_id); 


insert into product (product_id, name) values (1, 'HDD 250 gb'); 
insert into product (product_id, name) values (2, 'HDD 500 gb'); 
insert into product (product_id, name) values (3, 'HDD 750 gb'); 

insert into producer (producer_id, name) values (1, 'Hitachi'); 
insert into producer (producer_id, name) values (2, 'Corsair'); 
insert into producer (producer_id, name) values (3, 'Western Digital'); 

insert into catalog (pp_product_id, pp_producer_id, price) values (1,1, 80); 
insert into catalog (pp_product_id, pp_producer_id, price) values (1,3, 60); 
insert into catalog (pp_product_id, pp_producer_id, price) values (2,1, 75); 
insert into catalog (pp_product_id, pp_producer_id, price) values (2,2, 40); 
insert into catalog (pp_product_id, pp_producer_id, price) values (3,2, 63); 
insert into catalog (pp_product_id, pp_producer_id, price) values (3,3, 100); 

因此,有6個產品總共3個生產者,3類產品(HD類型的)。我需要的是顯示產品的CHEAPEST,每種類型的產品都帶有與該產品相關的生產商名稱。喜歡的東西:

西數250 GB 60

海盜船500GB的40

海盜船750GB的63

這將在他們的小組中選擇最便宜的HD(類型)

select p.name, min (c.price) 
from product p, catalog c, producer prc 
where c.pp_product_id = p.product_id and prc.producer_id = c.pp_producer_id 
group by p.name; 

...但我不能添加生產者名稱?我嘗試了幾種解決方案,但都沒有成功

+0

您不能添加生產者名稱,因爲它不是組的一部分 – evgenyl 2013-03-25 19:00:49

回答

2

這對row_number()很好用。您的數據不具有type場,所以我發明了一個在產品表:

select * 
from (select p.name as product_name, p.type, prod.name as producer_name, 
      price, 
      ROW_NUMBER() over (partition by p.name order by price) as seqnum 
     from catalog c join 
      product p 
      on c.pp_product_id = p.product_id join 
      producer prod 
      on c.pp_producer_id = prod.producer_id 
    ) t 
where seqnum = 1; 

如果你想所有產品的最低價格,用這個微小的變化:

select * 
from (select p.name as product_name, p.type, prod.name as producer_name, 
      price, 
      min(price) over (partition by p.name) as minprice 
     from catalog c join 
      product p 
      on c.pp_product_id = p.product_id join 
      producer prod 
      on c.pp_producer_id = prod.producer_id 
    ) t 
where price = minprice; 
+0

它說:SQL錯誤:ORA-00904:「PR」。「PRODUCER_ID」:無效標識符 00904. 00000 - 「%s:無效標識符」? – 2013-03-25 19:09:40

+0

實際上,產品名稱如「HDD 250 gb」應視爲 – 2013-03-25 19:11:43

+0

類型略有變化的解決方法是:由p分區。名稱按價格順序)從目錄ÇSEQNUM 加入 產品p 上c.pp_product_id = p.product_id上c.pp_producer_id = prod.producer_id 加入 生產者PROD )噸 其中SEQNUM = 1; – 2013-03-25 19:12:59

1

謝謝去@Gordon Linoff--他的代碼略微改變了它對我的問題的作用。只是爲了清楚起見,我在這裏張貼的解決方案:同時

select * 
from (select p.name as product_name, prod.name as producer_name,price, 
ROW_NUMBER() over (partition by p.name order by price) as seqnum 
from catalog c join 
product p 
on c.pp_product_id = p.product_id join 
producer prod 
on c.pp_producer_id = prod.producer_id 
) t 
where seqnum = 1; 
0

,我發現了一個另一種可能的答案:

SELECT p.name, pr.name, c2.minprice 
FROM product p, producer pr, catalog c, 
    (SELECT pp_product_id, MIN(price) AS minprice 
    FROM catalog c1 
    GROUP BY pp_product_id 
) c2 
WHERE p.product_id=c.pp_product_id 
AND pr.producer_id=c.pp_producer_id 
AND p.product_id =c2.pp_product_id 
AND c.price  =c2.minprice; 

輸出是這樣的:

硬盤750 GB海盜船6300

HDD 500 gb Corsair 4000

HDD 250 gb Western Digital 6000

相關問題