总结一个时间段的字数

summarize word count for a timeframe

我有下面的 table,它存储响应文本和与之关联的关键字搜索。

创建 table nlp.search(响应字符串,单词字符串,inquiry_time 时间戳);

插入 nlp.search 值('how to reset password','reset word password',TIMESTAMP ("2021-09-19 05:30:00+00")); 插入 nlp.search 值 ('how to reset password','reset passphrase',TIMESTAMP ("2021-09-20 07:30:00+00")); 插入 nlp.search 值 ('how to reset password','password',TIMESTAMP ("2021-09-16 08:30:00+00")); 插入 nlp.search 值('how to reset password','reset',TIMESTAMP ("2021-09-14 08:30:00+00"));

我想提供这种格式的总结报告 响应和与之关联的每个单词的计数。

回应individual_word_count
如何重置密码 reset(3) word(1) password(2) passphrase(1)

还可以传递时间戳列 inquiry_time 以缩小日期范围,并且必须相应地计算汇总值

例如,时间范围过滤器从 2021-09-19 到 2021-09-20

回应individual_word_count
如何重置密码 reset(2) word(1) password(1) passphrase(1)

这可以使用视图来完成吗?

下面使用

select response, word, count(1) ndividual_word_count
from `nlp.search`, 
unnest(split(words, ' ')) word
where date(inquiry_time) between '2021-09-19' and '2021-09-20'
group by response, word       

如果应用于您问题中的示例数据 - 输出为

I Need to display the word and count in 1 single column

然后在下面使用

select response, 
  string_agg(format('%s (%i)', word, individual_word_count)) counts
from (
  select response, word, count(1) individual_word_count
  from `nlp.search`, 
  unnest(split(words, ' ')) word
  where date(inquiry_time) between '2021-09-19' and '2021-09-20'
  group by response, word
)
group by response

有输出