如何在SQL WITH JOINS中使用聚合函数?

时间:2013-02-01 18:22:19

标签: sql oracle

以下是我为查找特定电影最便宜的价格以及销售它的经销商而构建的查询,如何操作它以便所有电影都以其各自的最高电影价格列出销售它的经销商?

/*finds the cheapest price for the movie "American Beauty" and the distributor who sells it*/
SELECT movies.title, movie_distributors.distributor_name, MIN(distributed_movie_list.unit_price) AS lowest_price 
FROM distributed_movie_list 
INNER JOIN movies ON distributed_movie_list.movie_id = movies.movie_id 
INNER JOIN movie_distributors ON movie_distributors.distributor_id = distributed_movie_list.distributor_id 
WHERE movies.title = 'American Beauty' 
AND distributed_movie_list.unit_price = 
    (SELECT MIN(unit_price) 
    FROM distributed_movie_list 
    INNER JOIN movies ON distributed_movie_list.movie_id = movies.movie_id 
    WHERE movies.title = 'American Beauty') 
GROUP BY movies.title, distributed_movie_list.distributor_id, movie_distributors.distributor_name;

以下表格用于此查询:

create table movies (
movie_id              number(5),
title                 varchar2(30) not null,
category_code         char(3) not null,
description           varchar2(500),
released_by           number(3) not null,
released_on           date not null
);

create table movie_distributors(
distributor_id      number(3),
distributor_name    varchar2(30) not null,
location            varchar2(40),
contact             varchar2(40)
);

create table distributed_movie_list(
distribution_id           number(8),
movie_id                  number(5) not null,
distributor_id            number(3) not null,
distribute_type           varchar2(10),
inventory_quantity        number(3) default 0,
unit_price                number(8,2)
);

我正在寻找的最终结果是一个查询,列出每部电影的发行商和最高价格。任何帮助将不胜感激 ; )

3 个答案:

答案 0 :(得分:2)

  

我正在寻找的最终结果是一个列出经销商的查询   每部电影的最高价格

那么为什么不只是GROUP BY title, distributor_nameMAX一样:

SELECT 
  m.movie_id,
  m.title,
  md.distributor_name, 
  MAX(d.unit_price) AS Highest_price 
FROM distributed_movie_list   AS d
INNER JOIN movies             AS m  ON d.movie_id        = m.movie_id 
INNER JOIN movie_distributors AS md ON md.distributor_id = d.distributor_id 
GROUP BY m.movie_id,m.title,
         md.distributor_name
HAVING MAX(d.unit_price) = (SELECT MAX(d2.unit_price)
                            FROM  distributed_movie_list d2
                            WHERE m.movie_id = d2.movie_id)

答案 1 :(得分:1)

以下内容适用于大多数RDBMS:

SELECT DISTINCT m.title, md.distributor_name, dml.unit_price AS highest_price 
FROM distributed_movie_list dml
INNER JOIN movies m ON dml.movie_id = m.movie_id 
INNER JOIN movie_distributors md ON md.distributor_id = dml.distributor_id 
INNER JOIN (SELECT movie_id, MAX(unit_price) 
            FROM distributed_movie_list
            GROUP BY movie_id) dmlh
        ON dml.unit_price = dmlH.unit_price AND dml.movie_id = dmlh.movie_id

对于支持排名功能的RDBMS(例如Oracle或SQLServer),应该可以使用更有效的解决方案。

答案 2 :(得分:1)

如果您尝试获得每部电影的最高价格 - 以及相关信息 - 请使用row_number()功能。以下查询返回有关每部电影的最高价格的所有信息:

select dml.*
from (select dml.*,
             ROW_NUMBER() over (partition by movie_id order by unit_price desc) as seqnum
      from distributed_movie_list dml 
     ) dml
where seqnum = 1

您可以加入有关电影和发行商的其他信息。