简体   繁体   English

Count和Distinct SQL查询

[英]Count and Distinct SQL query

I have a table of Books, that contains the following columns: 我有一个Books表,其中包含以下列:

Book_Id   User_Id
001       1
002       2
001       1
004       2
005       3
006       3
007       2
008       2
009       1

Where : 地点

Book_Id - identifier of a book that a user read; Book_Id - 用户阅读的书籍的标识符; User_Id - identifier of a reader/user. User_Id - 读者/用户的标识符。

Let's assume that User1 read books three times, but 2 of them were same, so the user 1 read 2 distinct books (001 and 009). 我们假设User1读了三次书,但其中有两本是相同的,所以用户1读了2本不同的书(001和009)。 User 2 read 4 distinct books, while user 3 read 2 distinct books. 用户2阅读4本不同的书籍,而用户3阅读2本不同的书籍。 In overall, there are 2 users that read 2 distinct books, and 1 user that read 4 distinct books. 总体而言,有2个用户阅读2本不同的书籍,1个用户阅读4本不同的书籍。 The output expected is as below: 预期产量如下:

Distinct_Books_Count --- User_Count
2                           2
4                           1

I tried the following: 我尝试了以下方法:

SELECT COUNT(DISTINCT Book_Id), COUNT(User_Id) FROM Books GROUP BY User_Id SELECT COUNT(DISTINCT Book_Id),COUNT(User_Id)FROM Books GROUP BY User_Id

But I receive the following table: 但我收到下表:

Distinct_Books_Count  User_Count
2                     3
4                     4
2                     2

So any alternative solution or changes? 那么任何替代解决方案或变化?

I call this a "histogram of histograms" query. 我称之为“直方图直方图”查询。 You can do it using two group by s: 您可以使用两个group by来执行此操作:

SELECT num_books, COUNT(*)
FROM (SELECT b.User_Id, COUNT(DISTINCT Book_Id) as num_books
      FROM Books b
      GROUP BY User_Id
     ) b
GROUP BY num_books
ORDER BY num_books;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM