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 类型)。我需要的是显示最便宜的产品,每种类型的产品都带有与该产品相关的生产商名称。就像是:

西部数据 250 GB 60

海盗船 500 GB 40

海盗船 750 GB 63

这将在他们的组(类型)中选择最便宜的高清

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;

...但我不能添加生产者姓名?我尝试了几种解决方案,但都没有奏效。

4

4 回答 4

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;
于 2013-03-25T19:04:08.400 回答
2

这应该回答您的核心问题。这篇文章解决了 MySQL 中的问题,但同样的概念也适用。

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

一旦您在组中获得最低价格的产品,其余的只是加入。

于 2013-03-25T19:01:16.800 回答
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;
于 2013-03-25T19:17:16.617 回答
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

硬盘 500 GB 海盗船 4000

硬盘 250 GB 西部数据 6000

于 2013-03-26T16:46:32.440 回答