我试图展示出发的唯一作者书籍数量最多的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
答案 0 :(得分: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 演示