繁体   English   中英

如何使用 Bigquery 通过前缀选择结构中的字段列表

[英]How to select a list of fields within a struct by prefix using Bigquery

我想做类似的事情:

SELECT
  STRUCT(
    c.customer.name*,
    r.revenue.type*
  ) AS customer_revenue
FROM customer_table c
JOIN revenue_table r
USING (transaction_id)

其中customer是一个包含许多字段的结构体,包括一些以“name”开头的字段(有些字段不是),而revenue同样是一个结构体,其中一些字段以“type”开头,一些字段不以“name”开头'不。

我似乎无法弄清楚如何在 BigQuery 中做到这一点。 我只是不断收到语法错误,让我相信这是不可能的。 有任何想法吗?

考虑以下替代方式

#standardSQL
SELECT (
  SELECT STRING_AGG(x) 
    FROM UNNEST(REGEXP_EXTRACT_ALL(TRIM(TO_JSON_STRING(c.customer), '{}'), r'("name\w+":\w+)(?:,|$)')) x
  ) || ',' || (
    SELECT STRING_AGG(y) 
    FROM UNNEST(REGEXP_EXTRACT_ALL(TRIM(TO_JSON_STRING(r.revenue), '{}'), r'("type\w+":\w+)(?:,|$)')) y
  ) AS customer_revenue
FROM customer_table c
JOIN revenue_table r
USING (transaction_id)  

在以下[超级简化]数据的情况下

#standardSQL
WITH customer_table AS (
  SELECT 1 transaction_id, STRUCT(1 AS name1, 2 AS name2, 3 AS aaa3, 4 AS bbb4) customer
), revenue_table AS (
  SELECT 1 transaction_id, STRUCT(11 AS type11, 22 AS type22, 33 AS type33, 44 AS ccc44) revenue
)

结果将是

Row customer_revenue     
1   "name1":1,"name2":2,"type11":11,"type22":22,"type33":33     

显然 - 这不完全是你在问题中问的 - 但正如我已经评论过的 - “真的不可行:o(你可能需要重新考虑你的要求” - 所以上述解决方案可能会帮助你做到这一点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM