2017-04-15 60 views
1

我需要得到這樣的(在Oracle SQL):SQL SELECT MIN()加入

Name   Producer    Min Price 
HDD 250 gb  Western Digital  6000 
HDD 500 gb  Corsair    4000 
HDD 750 gb  Corsair    6300 

,我可以用這個SQL

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

或與此得到它:

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; 

但我想使用SQL JOINS得到如下結果:

select a.name, b.name, min(c.price) 
from catalog c inner join product a on c.pp_product_id=a.product_id 
inner join producer b on c.pp_producer_id = b.producer_id 
group by a.name, b.name; 

但它不產生預期的結果,它輸出所有產品和生產者的價格。任何幫助?

DDL是:

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, 8000); 
insert into catalog (pp_product_id, pp_producer_id, price) values (1,3, 6000); 
insert into catalog (pp_product_id, pp_producer_id, price) values (2,1, 7500); 
insert into catalog (pp_product_id, pp_producer_id, price) values (2,2, 4000); 
insert into catalog (pp_product_id, pp_producer_id, price) values (3,2, 6300); 
insert into catalog (pp_product_id, pp_producer_id, price) values (3,3, 10000); 

回答

2

的問題不是join。這是最好的一排的選擇。要獲得最好的行使用row_number()(如果「最好」是重複的,則任意選擇)或rank()(重複時最好全選)。

select product, producer, price 
from (select a.name as product, b.name as producer, c.price, 
      row_number() over (partition by a.name order by c.price asc) as seqnum 
     from catalog c inner join 
      product a 
      on c.pp_product_id = a.product_id inner join 
      producer b 
      on c.pp_producer_id = b.producer_id 
    ) cpp 
where seqnum = 1; 
+0

它提供:ORA-00923:FROM關鍵字未找到預期 00923. 00000 - 「FROM關鍵字未找到預期」 *原因: *動作: 行錯誤:3欄:71 –

+0

@NenadBulatovic。 。 。這是一個錯字。 –

+0

嗯,我認爲使用SQL JOINS會比沒有它更簡單,現在我不確定它:) –