我正在尝试在 Google 的 BigQuery 中完成一项可能需要逻辑的任务,我不确定 SQL 是否可以本地处理。
我有 2 张桌子:
- 第一个表有一个单列,其中每一行都是一个小写单词
- 第二个表是评论数据库(包含发表评论的人、评论本身、时间戳等数据)
我想按第一个表中单词的出现次数对第二个表中的注释进行排序。
这是我想做的一个基本示例,使用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字……感觉不对。有小费吗?