首页 > 解决方案 > 在大查询中使用 STRUCT

问题描述

我在 BigQuery 中有一些问题,

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

客户ID 支付日期
啊啊啊 2021/01/23
啊啊啊 2021/01/24
bbb 2020/01/12

第二个表是 amount_table

Costemer_id 日期 概念ID 数量
啊啊啊 2021/01/23 主要的 1000
啊啊啊 2021/01/23 兴趣 200
啊啊啊 2021/01/24 滞纳金 30
啊啊啊 2021/01/24 主要的 1000
bbb 2020/01/12 主要的 250

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

客户ID 支付日期 概念标识 数量
啊啊啊 2021/01/23 主要的 100000
兴趣 200
啊啊啊 2021/01/24 滞纳金 30
主要的 10000
bbb 2020/01/12 主要的 250

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

select paymentdate.customer_id, paymentdate.paid_date, array(select struct(concept_id,amount) from amount_tablewhere amount_table.customer_id= paymentdate.customer_id and amount_table.paid_date= paymentdate.paid_date) frompaymentdate

任何人都可以帮忙吗?

标签: structgoogle-bigquery

解决方案


尝试在您的查询中替换paid_date为: 。date and amount_table.date=paymentdate.paid_date

还要考虑array_agg

select 
  paymentdate.customer_id,
  paymentdate.paid_date,
  array_agg(struct(concept_id,amount))
from paymentdate join amount_table 
  on paymentdate.customer_id = amount_table.customer_id
  and paymentdate.paid_date = amount_table.date
group by 1, 2

推荐阅读