首页 > 解决方案 > Big Query 中的 STRUCT 来自另一个表

问题描述

我在 BigQuery 中有一些问题,

我有 2 张桌子第一张桌子是 paymentSchedule

ID index_month 数量
啊啊啊 1 1000000
啊啊啊 2 2000000
bbb 1 500000
bbb 2 250万
bbb 3 2000000
ccc 1 2000000

第二个表是 BorrowerTable

ID 姓名
啊啊啊 亚历克斯
bbb 若尔德
ccc 胡安

我想使用 STRUCT 编写查询,我想要的结果是:

ID 姓名 index_month 数量
啊啊啊 亚历克斯 1 1000000
2 2000000
bbb 若尔德 1 500000
2 250万
3 2000000
ccc 胡安 1 2000000

我已经写了查询,但它不起作用

select *, STRUCT[select * from paymentSchedule where id=BorrowerTable.ID]from BorrowerTable

标签: sqlstructgoogle-bigquery

解决方案


with paymentSchedule as (
  select 'aaa' as id, 1 as index_month, 1000000 as amount union all
  select 'aaa', 2, 2000000 union all  
  select 'bbb', 1, 500000 union all  
  select 'bbb', 2, 25000000 union all    
  select 'bbb', 3, 20000000 union all    
  select 'ccc', 1, 20000000
),
BorrowerTable as (
  select 'aaa' as id, 'Alexa' as name union all
  select 'bbb', 'Jorde' union all
  select 'ccc', 'Juan'
)
select *, array(select struct(index_month, amount) from paymentSchedule where id=BorrowerTable.ID)
from BorrowerTable

在此处输入图像描述


推荐阅读