Bigquery:STRUCT (*) 语法
Bigquery: STRUCT (*) syntax
如何在不指定名称的情况下自动将 STRUCT
应用到 table 中的所有字段?
无效示例:
WITH data as (
SELECT 'Alex' as name, 14 as age, 'something else 1' other_field
UNION ALL
SELECT 'Bert' as name, 14 as age, 'something else 2' other_field
UNION ALL
SELECT 'Chiara' as name, 13 as age, 'something else 3' other_field
)
SELECT AS STRUCT(SELECT * except (other_field) from data) as student_data
Returns: Error: Scalar subquery cannot have more than one column unless using SELECT AS STRUCT to build STRUCT values at [9:17]
然而这有效:
WITH data as (
SELECT 'Alex' as name, 14 as age, 'something else 1' other_field
UNION ALL
SELECT 'Bert' as name, 14 as age, 'something else 2' other_field
UNION ALL
SELECT 'Chiara' as name, 13 as age, 'something else 3' other_field
)
SELECT STRUCT(name,age) as student_data
from data
问题是,一旦我有 100 列,其中只有 5 列不属于,把它们写出来会让我发疯。有没有更简单的方法来使用某些版本的 Select * Except()
?
下面是 BigQuery 标准 SQL
#standardSQL
WITH data AS (
SELECT 'Alex' AS name, 14 AS age, 'something else 1' other_field UNION ALL
SELECT 'Bert' AS name, 14 AS age, 'something else 2' other_field UNION ALL
SELECT 'Chiara' AS name, 13 AS age, 'something else 3' other_field
)
SELECT (
SELECT AS STRUCT * EXCEPT(other_field)
FROM UNNEST([t])
) AS student_data
FROM data t
输出
Row student_data.name student_data.age
1 Alex 14
2 Bert 14
3 Chiara 13
您需要 AS STRUCT expr
的表达式
SELECT AS STRUCT data.* except (other_field) from data
作为对 Mikhail Berlyant 回答的更新 因为评论部分不允许我使用正确的格式:
请注意,您可能需要将 SELECT AS STRUCT * EXCEPT(other_field)
括在 () 中。 UNNEST([t]) 部分不是必需的。 IE。这也有效:
#standardSQL
WITH data AS (
SELECT 'Alex' AS name, 14 AS age, 'something else 1' other_field UNION ALL
SELECT 'Bert' AS name, 14 AS age, 'something else 2' other_field UNION ALL
SELECT 'Chiara' AS name, 13 AS age, 'something else 3' other_field
)
SELECT
(SELECT AS STRUCT data.* EXCEPT(other_field)) as student_data,
FROM data
如何在不指定名称的情况下自动将 STRUCT
应用到 table 中的所有字段?
无效示例:
WITH data as (
SELECT 'Alex' as name, 14 as age, 'something else 1' other_field
UNION ALL
SELECT 'Bert' as name, 14 as age, 'something else 2' other_field
UNION ALL
SELECT 'Chiara' as name, 13 as age, 'something else 3' other_field
)
SELECT AS STRUCT(SELECT * except (other_field) from data) as student_data
Returns: Error: Scalar subquery cannot have more than one column unless using SELECT AS STRUCT to build STRUCT values at [9:17]
然而这有效:
WITH data as (
SELECT 'Alex' as name, 14 as age, 'something else 1' other_field
UNION ALL
SELECT 'Bert' as name, 14 as age, 'something else 2' other_field
UNION ALL
SELECT 'Chiara' as name, 13 as age, 'something else 3' other_field
)
SELECT STRUCT(name,age) as student_data
from data
问题是,一旦我有 100 列,其中只有 5 列不属于,把它们写出来会让我发疯。有没有更简单的方法来使用某些版本的 Select * Except()
?
下面是 BigQuery 标准 SQL
#standardSQL
WITH data AS (
SELECT 'Alex' AS name, 14 AS age, 'something else 1' other_field UNION ALL
SELECT 'Bert' AS name, 14 AS age, 'something else 2' other_field UNION ALL
SELECT 'Chiara' AS name, 13 AS age, 'something else 3' other_field
)
SELECT (
SELECT AS STRUCT * EXCEPT(other_field)
FROM UNNEST([t])
) AS student_data
FROM data t
输出
Row student_data.name student_data.age
1 Alex 14
2 Bert 14
3 Chiara 13
您需要 AS STRUCT expr
SELECT AS STRUCT data.* except (other_field) from data
作为对 Mikhail Berlyant 回答的更新
请注意,您可能需要将 SELECT AS STRUCT * EXCEPT(other_field)
括在 () 中。 UNNEST([t]) 部分不是必需的。 IE。这也有效:
#standardSQL
WITH data AS (
SELECT 'Alex' AS name, 14 AS age, 'something else 1' other_field UNION ALL
SELECT 'Bert' AS name, 14 AS age, 'something else 2' other_field UNION ALL
SELECT 'Chiara' AS name, 13 AS age, 'something else 3' other_field
)
SELECT
(SELECT AS STRUCT data.* EXCEPT(other_field)) as student_data,
FROM data