我正在尝试使用 SQL 在 Big Query 中进行一些数据转换。
假设我有三个表:
客户 - 有关客户的数据,例如年龄等 订阅 - 有关用户订阅的数据 参与度 - 有关客户如何与数字产品交互的数据。
我想使用嵌套字段将其收集在一个表中。
我可以加入所有这些表,但我想将它们聚合到数组中。
因此,我得到的不是三个表:
id:123,
name:David,
age:30,
subscritions: [{
name:sub1
price:10
},
{
name:sub2
price:20
}],
engagment: [{
event:visited_product_x
time:2020-06-10
},
{
event:visited_product_y
time:2020-06-10
}]
当然,我在 SELECT 中使用了 array_agg。当只添加一个表时,效果很好。但是,当添加另一个时,我会得到重复的行,这是我不想要的。所以,我想我不应该在 SELECT 中使用 array_agg,而应该在其他地方使用。
但是解决这个问题的最佳方法是什么?