统计不同的共现

Pet*_*r O 2 sql oracle

我有一个数据库,其中包含文档列表及其中的单词.每行代表一个术语.我要做的是计算一个单词出现的文档数量.

所以,鉴于以下内容:

+  doc  +  word  +
+-------+--------+
+   a   +  foo   +
+-------+--------+
+   a   +  foo   +
+-------+--------+
+   a   +  bar   +
+-------+--------+
+   b   +  bar   +
+-------+--------+
Run Code Online (Sandbox Code Playgroud)

我得到了结果

+  word  +  count  +
+--------+---------+
+  foo   +    1    +
+--------+---------+
+  bar   +    2    +
+--------+---------+
Run Code Online (Sandbox Code Playgroud)

因为foo只出现在一个文档中(即使它在该文档中出现两次),并且bar出现在两个文档中.

从本质上讲,我应该做的是(以为)以下查询吐出的单词的COUNT,

SELECT DISTINCT word, doc FROM table
Run Code Online (Sandbox Code Playgroud)

..但我无法弄明白.任何提示?

And*_*mar 5

你可以distinct在里面使用count,比如:

select  word
,       count(distinct doc)
from    YourTable
group by
        word
Run Code Online (Sandbox Code Playgroud)