0

如何在 Oracle 中编写 sql 查询以获得以下结果:

表“books”有五列:BOOK_ID、BOOK_TITLE、BOOK_AUTHOR、PRICE、PUBLISH_DATE。过去 300 天内每位作者出版的最昂贵书籍的打印清单?

select book_author, 
       publish_date 
  from books 
 where max(price) and publish_date between (sysdate-300) and sysdate;
4

2 回答 2

1

有几种方法可以实现您的目标。这是一个自排除连接,应该相当有效。它基本上是说以没有更高价格的价格将行归还给我;对于每个作者。这里有一个经过测试的示例:http ://sqlfiddle.com/#!4/86b22/1

select 
    b.book_author, b.publish_date, b.price
from 
    books b
    left join books b2
    on b.book_author = b2.book_author
    and b2.price > b.price
    and b2.publish_date between (sysdate-300) and sysdate
where 
    b.publish_date between (sysdate-300) and sysdate
    and b2.book_id is null;

如果您有兴趣,可以在我的博客上找到更多此类查询的示例:http: //adam-bernier.appspot.com/post/38001/the-self-exclusion-join

于 2013-01-29T23:26:20.733 回答
1

您也可以使用分析来完成此操作:

select * 
  from (select book_id,
               book_author,
               price,
               publish_date,
               row_number() over (partition by book_author order by price desc) rn
          from books
         where publish_date >= (sysdate - 300)
       )
 where rn = 1;
于 2013-01-29T23:35:02.197 回答