【问题标题】:SQL query to count distinct count from 2 columnsSQL查询计算2列的不同计数
【发布时间】:2023-03-20 14:51:01
【问题描述】:

我有一个名为 table 的 SQL 表

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

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

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

谁能帮我编写将返回此结果的查询?

【问题讨论】:

    标签: sql sql-server database


    【解决方案1】:

    你想要count(distinct):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 2023-02-26
      • 2012-06-08
      • 2019-08-11
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多