SQL查询,从两列中分别计数

时间:2020-12-15 15:41:45

I have SQL table named table

我有一个名为table的SQL表

Document    Word
-------------------
doc1    Hello
doc1    Hi
doc1    Welcome
doc1    Hello
doc2    Welcome
doc2    Welcome
doc3    Hi
doc3    Hello
doc3    Good Luck

This means I have list of documents, each document contains some words, and I have raw for each word per each document, and if a word appeared twice in the same document I will have 2 raws. I want to get a list of distinct words with how many documents it appeared in, regardless how many times it appeared. So the output from this table should be

这意味着我有文档列表,每个文档都包含一些单词,每个文档都有一个单词的raw格式,如果一个单词在同一个文档中出现两次,我将有两个raws。我想要得到一个不同单词的列表,不管它出现了多少次。这个表的输出应该是

Hello: 2 --> which means it appeared in 2 documents
Hi: 2
Welcome: 2
Good Luck: 1

Can anyone please help me in writing the query that will return this result?

谁能帮我写一下会返回这个结果的查询?

1 个解决方案

#1


3  

You want count(distinct):

你想要计数(不同的):

select word, count(distinct document)
from t
group by word;

#1


3  

You want count(distinct):

你想要计数(不同的):

select word, count(distinct document)
from t
group by word;