Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何计算从一个表到另一个表中注释的单词出现次数_Sql_Google Bigquery_Bigquery Standard Sql - Fatal编程技术网

Sql 如何计算从一个表到另一个表中注释的单词出现次数

Sql 如何计算从一个表到另一个表中注释的单词出现次数,sql,google-bigquery,bigquery-standard-sql,Sql,Google Bigquery,Bigquery Standard Sql,我试图在Google的BigQuery中完成一项任务,这可能需要逻辑,但我不确定SQL是否能够以本机方式处理 我有两张桌子: 第一个表有一个单独的列,其中每一行都是一个小写单词 第二个表是一个评论数据库,其中包含诸如谁发表了评论、评论本身、时间戳等数据 我希望根据第一个表中出现的单词数量对第二个表中的注释进行排序 下面是我想做的一个基本示例,使用python,使用字母而不是单词。。。但你明白了: words = ['a','b','c','d','e'] comments = ['this i

我试图在Google的BigQuery中完成一项任务,这可能需要逻辑,但我不确定SQL是否能够以本机方式处理

我有两张桌子:

第一个表有一个单独的列,其中每一行都是一个小写单词 第二个表是一个评论数据库,其中包含诸如谁发表了评论、评论本身、时间戳等数据 我希望根据第一个表中出现的单词数量对第二个表中的注释进行排序

下面是我想做的一个基本示例,使用python,使用字母而不是单词。。。但你明白了:

words = ['a','b','c','d','e']

comments = ['this is the first sentence', 'this is another comment', 'look another sentence, which is also a comment', 'nope', 'no', 'run']

wordcount = {}

for comment in comments:
    for word in words:
        if word in comment:
            if comment in wordcount:
                wordcount[comment] += 1
            else:
                wordcount[comment] = 1

print(sorted(wordcount.items(), key = lambda k: k[1], reverse=True))
输出:

[('look another sentence, which is also a comment', 3), ('this is another comment', 3), ('this is the first sentence', 2), ('nope', 1)]
到目前为止,我所看到的生成SQL查询的最好方法是执行以下操作:

SELECT
    COUNT(*)
FROM
    table
WHERE
    comment_col like '%word1%'
    OR comment_col like '%word2%'
    OR ...

但是有超过2000个单词。。。只是感觉不对。有什么建议吗?

如果我理解得很好,我想您需要这样的查询:

select comment, count(*) cnt
from comments
join words
  on comment like '% ' + word + ' %'   --this checks for `... word ..`; a word between spaces
  or comment like word + ' %'          --this checks for `word ..`; a word at the start of comment
  or comment like '% ' + word          --this checks for `.. word`; a word at the end of comment
  or comment = word                    --this checks for `word`; whole comment is the word
group by comment
order by count(*) desc

下面是BigQuery标准SQL

标准SQL 选择注释,将单词计数为cnt 根据评论 连接词 在strpsocomment上,word>0 按评论分组 -按cnt DESC订购 如果您愿意,您可以选择使用regexp:

标准SQL 选择注释,将单词计数为cnt 根据评论 连接词 在REGEXP_中包含注释,word 按评论分组 -按cnt DESC订购 您可以使用问题中的虚拟示例测试/玩上面的内容

标准SQL 以文字为中心 选择单词 从UNNEST[a','b','c','d','e']单词 , 评论为 选择注释 FROM UNNEST[“这是第一句话”,“这是另一条评论”,“看另一句话,这也是一条评论”,“nope”,“no”,“run”]评论 选择注释,将单词计数为cnt 根据评论 连接词 在strpsocomment上,word>0 按评论分组 按cnt DESC订购 更新内容:

你有没有什么快速的建议只进行完整的字符串匹配

标准SQL 以文字为中心 选择单词 来自UNNEST[a'、'no'、'is'、'd'、'e']单词 , 评论为 选择注释 FROM UNNEST[“这是第一句话”,“这是另一条评论”,“看另一句话,这也是一条评论”,“nope”,“no”,“run”]评论 选择注释,将单词计数为cnt 根据评论 连接词 在REGEXP_中包含注释,CONCATr'\b',word,r'\b' 按评论分组 按cnt DESC订购
当你使用字母而不是单词时,我能理解你为什么会得到这样的输出!谢谢米哈伊尔,我相信这是我所说的工作。但是我发现子串匹配太多了。你有没有什么快速的建议只进行完整的字符串匹配?此外,我并不局限于标准SQL,我认为这是BigQuery唯一支持的东西,但它似乎可以做得更多。你是最好的!谢谢你的帮助。我将继续阅读更多SQL: