Google bigquery 在Google BigQuery中按索引添加向量
我有这样一个数据集:Google bigquery 在Google BigQuery中按索引添加向量,google-bigquery,Google Bigquery,我有这样一个数据集: id | house_id | room_data ------------------------------------------- 1 | abc | [1, 1, 1, 1, 1, 1, 1] 2 | abc | [1, 1, 0, 0, 0, 0, 0] 3 | abc | [0, 1, 2, 1, 0, 1, 0] 4 | abc | [1, 1, 1, 0, 1, 1, 1] 5 | def | [
id | house_id | room_data
-------------------------------------------
1 | abc | [1, 1, 1, 1, 1, 1, 1]
2 | abc | [1, 1, 0, 0, 0, 0, 0]
3 | abc | [0, 1, 2, 1, 0, 1, 0]
4 | abc | [1, 1, 1, 0, 1, 1, 1]
5 | def | [1, 1, 1, 1, 0, 0, 0]
6 | def | NULL
7 | def | [1, 1, 1, 1, 0, 0, 0, 2, 1]
8 | def | [1, 0, 0, 0, 1, 1, 1, 1, 0]
house_id | aggregate_room_data
--------------------------------
abc | [3, 4, 4, 2, 2, 3, 2]
我想选择与给定的房屋id
相关的向量,并创建一个新的聚合向量,该聚合向量通过其索引
对于abc
的house\u id
查询的示例结果理想情况下如下所示:
id | house_id | room_data
-------------------------------------------
1 | abc | [1, 1, 1, 1, 1, 1, 1]
2 | abc | [1, 1, 0, 0, 0, 0, 0]
3 | abc | [0, 1, 2, 1, 0, 1, 0]
4 | abc | [1, 1, 1, 0, 1, 1, 1]
5 | def | [1, 1, 1, 1, 0, 0, 0]
6 | def | NULL
7 | def | [1, 1, 1, 1, 0, 0, 0, 2, 1]
8 | def | [1, 0, 0, 0, 1, 1, 1, 1, 0]
house_id | aggregate_room_data
--------------------------------
abc | [3, 4, 4, 2, 2, 3, 2]
以及具有def
的house\u id
的查询的示例结果:
house_id | aggregate_room_data
--------------------------------------
def | [3, 2, 2, 2, 1, 1, 1, 3, 1]
我相信我需要一些ARRAY\u AGG
,交叉连接UNNEST
,求和
的组合,也许还有带偏移量的,但我是个傻瓜,只是不能正确理解语法,尽管我想我在这里圈出了答案
提前感谢您的帮助以下是BigQuery标准SQL
#standardSQL
select house_id,
array_agg(data order by offset) as aggregate_room_data
from (
select house_id, offset, sum(data) data
from `project.dataset.table`,
unnest(room_data) data with offset
where not room_data is null
group by house_id, offset
)
group by house_id
如果要应用于问题中的样本数据,则输出为
房间数据列的数据类型是什么?它是整数数组还是字符串?@MikhailBerlyant它是整数数组