| Word | ID |
-------------------
|computer | 1 |
|computer | 1 |
|computer | 1 |
|computer | 2 |
|Printer | 1 |
|Printer | 2 |
| Word | WordCount |
-------------------------
|computer | 2 |
|Printer | 2 |
一个单词可以在表中具有多次相同的ID。我可以得到每个单词出现的次数:
select
distinct(word),count(word) as WordCount
from
table1
group by word
但是,如果Word和ID列相同,我不知道如何只计算一次记录。
这是我最接近的:
select distinct(word), id from table1
这几乎给了我想要的东西,它每个ID返回一个单词但我需要每个单词的Count。
所以我的问题是如何计算每个单词,同时排除具有相同单词和ID的行,在计算第一个单词和ID之后。
谢谢!
答案 0 :(得分:1)
您需要在COUNT中使用DISTINCT:
select
distinct(word),count(distinct word) as WordCount
from
table1
group by word
请参阅http://www.w3schools.com/sql/sql_func_count.asp中的SQL COUNT(DISTINCT column_name)语法。
答案 1 :(得分:0)
您可以像这样使用Sub Select:
select word, count(word) as WordCount FROM (
select
word
from
table1
group by word, id
) t1 group by word