在Google BigQuery上(使用#standardSQL),当两个表之间存在连接时,我需要将固定前缀应用于每个表的所有列。
这是场景,我有这样的结构:
#standardSQL
WITH user AS (
SELECT "john" as name, "smith" as surname, 1 as parent
UNION ALL
SELECT "maggie" as name, "smith" as surname, 2 as parent
),
parent AS (
SELECT 1 as id, "john" as name, "doe" as surname
UNION ALL
SELECT 2 as id, "jane" as name, "smith" as surname
)
用户表
+-----+--------+---------+--------+
| Row | name | surname | parent |
+-----+--------+---------+--------+
| 1 | john | smith | 1 |
| 2 | maggie | smith | 2 |
+-----+--------+---------+--------+
父表
+-----+----+------+---------+
| Row | id | name | surname |
+-----+----+------+---------+
| 1 | 1 | john | doe |
| 2 | 2 | jane | smith |
+-----+----+------+---------+
像这样的查询
SELECT u.*, p.* FROM user u JOIN parent p ON u.parent = p.id
产生以下错误
Error: Duplicate column names in the result are not supported. Found duplicate(s): name, surname
我想避免像这样执行表格的自定义别名
SELECT
u.name as user_name,
u.surname as user_surname,
p.name as parent_name,
p.surname as parent_surname
FROM user u JOIN parent p ON u.parent = p.id
+-----+-----------+--------------+-------------+----------------+
| Row | user_name | user_surname | parent_name | parent_surname |
+-----+-----------+--------------+-------------+----------------+
| 1 | john | smith | john | doe |
| 2 | maggie | smith | jane | smith |
+-----+-----------+--------------+-------------+----------------+
如果表格会在字段上发生变化,我每次都需要编辑语句(或语句),以便应用具有给定前缀的新字段。所以这种使用固定列名称的方法不合适
有没有办法,一个查询运算符,为了获得那里提到的表,自动应用前缀?类似的东西:
SELECT u.* AS user_*, p.* AS parent_*
FROM user u JOIN parent p ON u.parent = p.id
答案 0 :(得分:3)
到目前为止,我能想到的唯一选择是
#standardSQL
WITH user AS (
SELECT "john" AS name, "smith" AS surname, 1 AS parent UNION ALL
SELECT "maggie" AS name, "smith" AS surname, 2 AS parent
), parent AS (
SELECT 1 AS id, "john" AS name, "doe" AS surname UNION ALL
SELECT 2 AS id, "jane" AS name, "smith" AS surname
)
SELECT user, parent
FROM user
JOIN parent
ON user.parent = parent.id
结果为
Row user.name user.surname user.parent parent.id parent.name parent.surname
1 john smith 1 1 john doe
2 maggie smith 2 2 jane smith
它不是您所期望的,但最接近它,因为它将各个连接表中的每一行包装到相应的STRUCT中 - 例如:
{
"user": {"name": "john", "surname": "smith","parent": "1"},
"parent": {"id": "1","name": "john","surname": "doe"}
}