I have the following table that contains nested STRUCTs, and in a subquery, I am trying to add additional columns at the struct level. I've created a reproducible example of my efforts thus far:
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
The current code throws an error Syntax error: Unexpected "." at [18:70] at line 18 (with the safe_divide). If I switch in line 17 / out line 18, the code works, but then fg_pct is not in the t1.o struct, where i'd like it to be.
Is there any way to add columns into nested structs in subqueries like this?