BigQuery 将新列添加到子查询中的嵌套 STRUCT

Posted

技术标签:

【中文标题】BigQuery 将新列添加到子查询中的嵌套 STRUCT【英文标题】:BigQuery add new columns into nested STRUCT in subquery 【发布时间】:2019-11-10 04:03:00 【问题描述】:

我的下表包含嵌套的STRUCTs,并且在子查询中,我试图在结构级别添加其他列。到目前为止,我已经创建了一个可重复的示例:

WITH wide_stats AS (
  (
    SELECT 
      'joe' name, 'bills' team,
      struct(struct(7 as fga, 5 as fgm) as o, struct(8 as fga, 3 as fgm) as d) as t1,
      struct(struct(3 as fga, 4 as fgm) as o, struct(9 as fga, 2 as fgm) as d) as t2
  ) UNION ALL (
    SELECT 'nick' name, 'jets' team,
      struct(struct(12 as fga, 7 as fgm) as o, struct(13 as fga, 7 as fgm) as d) as t1,
      struct(struct(15 as fga, 7 as fgm) as o, struct(22 as fga, 7 as fgm) as d) as t2
  )
)

SELECT 
  *,
--   safe_divide(wide_stats.t1.o.fgm, wide_stats.t1.o.fga) as fg_pct,
  safe_divide(wide_stats.t1.o.fgm, wide_stats.t1.o.fga) as wide_stats.t1.o.fg_pct  

FROM wide_stats

当前代码在第 18 行(使用 safe_divide)抛出错误 Syntax error: Unexpected "." at [18:70]。如果我在第 17 行/第 18 行切换,代码可以工作,但是 fg_pct 不在 t1.o 结构中,我希望它在哪里。

有没有办法在这样的子查询中将列添加到嵌套结构中?

【问题讨论】:

【参考方案1】:

以下是 BigQuery 标准 SQL

#standardSQL
WITH wide_stats AS (
  SELECT 'joe' name, 'bills' team,
    STRUCT(STRUCT(7 AS fga, 5 AS fgm) AS o, STRUCT(8 AS fga, 3 AS fgm) AS d) AS t1,
    STRUCT(STRUCT(3 AS fga, 4 AS fgm) AS o, STRUCT(9 AS fga, 2 AS fgm) AS d) AS t2 UNION ALL 
  SELECT 'nick' name, 'jets' team,
    STRUCT(STRUCT(12 AS fga, 7 AS fgm) AS o, STRUCT(13 AS fga, 7 AS fgm) AS d) AS t1,
    STRUCT(STRUCT(15 AS fga, 7 AS fgm) AS o, STRUCT(22 AS fga, 7 AS fgm) AS d) AS t2
)
SELECT * REPLACE (
  (SELECT AS STRUCT t1.* REPLACE (
    (SELECT AS STRUCT t1.o.*, SAFE_DIVIDE(wide_stats.t1.o.fgm, wide_stats.t1.o.fga) AS fg_pct ) 
    AS o))
  AS t1)
FROM wide_stats   

结果

Row name    team    t1.o.fga    t1.o.fgm    t1.o.fg_pct         t1.d.fga    t1.d.fgm    t2.o.fga    t2.o.fgm    t2.d.fga    t2.d.fgm     
1   joe     bills   7           5           0.7142857142857143  8           3           3           4           9           2    
2   nick    jets    12          7           0.5833333333333334  13          7           15          7           22          7    

【讨论】:

有用的答案,但是当我尝试使用这种方法将percent_rank()SELECT AS STRUCT 一起使用时,我收到了这个错误SELECT without FROM clause cannot use analytic functions at [92:6] 在您的问题中看不到任何分析函数 - 您可能想发布包含所有详细信息的新问题,以便我们提供帮助:o)

以上是关于BigQuery 将新列添加到子查询中的嵌套 STRUCT的主要内容,如果未能解决你的问题,请参考以下文章

将 BigQuery 嵌套字段内容展平为新列而不是行

将新列添加到现有表中并使用 PL/SQL 中游标中的值更新它们

BigQuery:使用 python BQ API 向现有表添加新列

R中的高效方法是将新列添加到具有大数据集的数据框中

EntityFrameworkCore - 将新列添加到联接表

将新列添加到 wordpress 数据库