我想要做的是从BigQuery上的可用/公共trigram数据重新创建bigram数据。在此过程中,我想减少数据。这很难,因为在一行中似乎有一个数据列表,例如,cell.value是一个包含所有年份的列名,它可以包含100个元素,所有这些都是在一行。
我喜欢的列是这样的:
ngram,第一,第二,第三,cell.match_count *已修改
其中修改后的最后一列是2000 - 2008年所有匹配计数的总和(忽略所有旧数据)。我怀疑这会大大减少文件的大小(以及其他一些调整)。
到目前为止我的代码是(我必须为此运行2个单独的查询)
SELECT ngram, cell.value, cell.match_count
FROM [publicdata:samples.trigrams]
WHERE ngram = "I said this"
AND cell.value in ("2000","2001","2002","2003","2004","2005","2006","2007","2008")
SELECT ngram, SUM(cell.match_count) as total
FROM [one_syllable.test]
GROUP BY ngram
结果是2列,包含1行数据:我说过这个,1181
但是在我做一些修剪之前,我想为每一个ngram得到这个
如何合并查询以便一次完成所有查询并返回列first
,second
和third
?
谢谢!
PS我试过
SELECT ngram, cell.value, cell.match_count
FROM [publicdata:samples.trigrams]
WHERE cell.value in ("2000","2001","2002","2003","2004","2005","2006","2007","2008")
但我得到一个错误"响应太大而无法返回" ...
答案 0 :(得分:3)
错误“响应太大而无法返回”意味着您必须将结果写入目标表,并选中“允许大结果”。 BigQuery won't return more than 128MB直接不使用目标表。
您应该能够使用某些聚合函数生成所需的表。尝试“GROUP EACH BY ngram”并行聚合,并使用FIRST函数从第一列,第二列和第三列中选择一个值。它看起来像这样:
SELECT ngram, FIRST(first), FIRST(second), FIRST(third), SUM(cell.match_count)
FROM [publicdata:samples.trigrams]
WHERE cell.value in ("2000","2001","2002","2003","2004","2005","2006","2007","2008")
GROUP EACH BY ngram;
答案 1 :(得分:0)
Google BIGQUERY 现在在免费 trigrams 数据集上有数组,需要修改原始答案以使用 UNNEST 函数展平数组(在本例中为单元格)。修改了下面的示例 SQL 代码。
SELECT t1.ngram, t1.first, t1.second, t1.third, SUM(c.match_count)
from bigquery-public-data.samples.trigrams t1, UNNEST(cell) as c
WHERE {"2000","2001","2002","2003","2004","2005","2006","2007","2008"} IN
UNNEST(c.value)
GROUP BY 1,2,3,4;