2017-04-15 9 views
1

に参加:SQL選択分は()私は(のOracle SQLで)このような結果必要

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を: 00923. 00000を期待どこキーワードは見つかりません - *原因 "キーワードから期待されるが見つかりません": *アクション:ラインで エラー:3カラム:71 –

+0

を@NenadBulatovic。 。 。それはタイプミスでした。 –

+0

ええと、私はSQL JOINSを使用するとそれがなくても簡単になると思っていました。それについてはわかりません:) –

関連する問題