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 に答える