SQL select min()使用SQL join

时间:2017-04-15 15:53:57

标签: sql oracle

我需要这样的结果(在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);

1 个答案:

答案 0 :(得分: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;