← 返回 meta 的题目列表Top 3 Customers by Book Purchases in Specific Categories
类型:online_judge
In categories where conditions are met, select the top three customers by the number of books purchased. The category condition is that it must contain books from at least three different authors.
Input:
A database table Purchases, with fields: CustomerId, BookId, Category.
A database table Books, with fields: BookId, Author.
Output:
Return the top three customers by the number of books purchased in categories that meet the condition.
Requirements:
Ignore categories that do not have books from at least three different authors.
If there is a tie in the number of books purchased, any three customers can be chosen.
Example: Suppose the Purchases table is as follows: | CustomerId | BookId | Category | |------------|--------|----------| | 1 | 101 | Fiction | | 2 | 102 | Fiction | | 1 | 103 | Fiction | | 3 | 104 | Fiction |
And the Books table is as follows: | BookId | Author | |--------|---------------| | 101 | Author A | | 102 | Author B | | 103 | Author C | | 104 | Author A |
In this example, the Fiction category meets the condition since it includes books from three different authors. The possible result could be [1, 2, 3].
Example
Input
purchases: (CustomerId,BookId,Category)
1 101 Fiction
2 102 Fiction
1 103 Fiction
3 104 Fiction
books: (BookId,Author)
101 Author A
102 Author B
103 Author C
104 Author A