2

我正在尝试显示出版的唯一作者书籍数量最多的 I_PUBLISHER,如何在 SQL 语句中实现这一点?

设想:

 Publisher A publishes 10 books with 10 unique author
 Publisher B publishes 10 book with that 10 book from same author.

 BOOK_ID, BOOK_TITLE, BOOK_AUTHOR_ID, BOOK_PUBLISHER,

 SQL statement to get the publisher name with largest number of UNIQUE author 
4

1 回答 1

1

尝试

SELECT * 
  FROM
(
  SELECT book_publisher, COUNT(DISTINCT book_author_id) author_count
    FROM table1
   GROUP BY book_publisher
   ORDER BY author_count DESC 
)
 WHERE rownum = 1

或者

WITH cte AS
(
  SELECT book_publisher, COUNT(DISTINCT book_author_id) author_count
    FROM table1
   GROUP BY book_publisher
)
SELECT book_publisher
  FROM cte
 WHERE author_count = 
(
  SELECT MAX(author_count) 
    FROM cte
)

或具有分析功能

SELECT book_publisher, author_count 
  FROM
(
  SELECT book_publisher,
         COUNT(DISTINCT book_author_id) author_count,
         DENSE_RANK() OVER (ORDER BY COUNT(DISTINCT book_author_id) DESC) rank
    FROM table1
   GROUP BY book_publisher
)
 WHERE rank = 1

这是SQLFiddle演示

于 2013-08-24T05:04:33.820 回答