Sql 在BigQuery中使用合并时无法识别名称
我正在编写一个标准查询来运行BigQuery中的各种不同表 有些表将包含有关事务的数据,而其他表则不会 重要的是,我的最后一个表包含transactions列,即使源中不存在键 我想使用COALESCE检查事务键是否存在,如果不存在,则使用0作为值 标准SQL 选择 CONCAT'googleanalytics',格式为日期时间%Y%m%d,日期时间日期,UTC,设备类别,sourcemedium,活动作为键, “谷歌分析”作为数据来源, 会议, 浏览量, 反弹, 合并事务,0作为事务, 目标完成所有, 从…起 `project.datasource.table` 当我的源表不包含“transactions”键时,我希望查询运行,但使用0作为值 相反,我得到了一个错误: 无法识别的名称:交易时间[10:13]Sql 在BigQuery中使用合并时无法识别名称,sql,google-bigquery,Sql,Google Bigquery,我正在编写一个标准查询来运行BigQuery中的各种不同表 有些表将包含有关事务的数据,而其他表则不会 重要的是,我的最后一个表包含transactions列,即使源中不存在键 我想使用COALESCE检查事务键是否存在,如果不存在,则使用0作为值 标准SQL 选择 CONCAT'googleanalytics',格式为日期时间%Y%m%d,日期时间日期,UTC,设备类别,sourcemedium,活动作为键, “谷歌分析”作为数据来源, 会议, 浏览量, 反弹, 合并事务,0作为事务, 目标完
我明白为什么会这样,但我需要绕开它。有什么想法吗?给表格加上别名,然后使用该别名引用交易:
SELECT
CONCAT('googleanalytics', FORMAT_DATETIME('%Y%m%d', DATETIME(t.date, 'UTC')),
t.devicecategory, t.sourcemedium, t.campaign) AS key,
'Google Analytics' AS data_source,
t.sessions,
t.pageviews,
t.bounces,
COALESCE(t.transactions, 0) AS transactions,
t.goalcompletionsall
FROM `project.datasource.table` t;
为表添加别名,然后使用该别名引用事务:
SELECT
CONCAT('googleanalytics', FORMAT_DATETIME('%Y%m%d', DATETIME(t.date, 'UTC')),
t.devicecategory, t.sourcemedium, t.campaign) AS key,
'Google Analytics' AS data_source,
t.sessions,
t.pageviews,
t.bounces,
COALESCE(t.transactions, 0) AS transactions,
t.goalcompletionsall
FROM `project.datasource.table` t;
您确实不应该使用不在表中的列编写查询。我建议你把桌子修好 但是,如果您的表在BQ中有一个主键,那么您可以使用以下作用域技巧:
select . . .,
(select (select transactions -- NO alias! This is to trick scoping
from `project.datasource.table` t2
where t2.primary_key = t.primary_key
)
from (select 0 as transactions) tr
) as transactions
from `project.datasource.table` t;
注意:我在其他数据库中使用过这个,但在BigQuery中没有专门使用过。您确实不应该使用不在表中的列编写查询。我建议你把桌子修好 但是,如果您的表在BQ中有一个主键,那么您可以使用以下作用域技巧:
select . . .,
(select (select transactions -- NO alias! This is to trick scoping
from `project.datasource.table` t2
where t2.primary_key = t.primary_key
)
from (select 0 as transactions) tr
) as transactions
from `project.datasource.table` t;
注意:我已经在其他数据库中使用过,但没有在BigQuery中专门使用过。下面是BigQuery标准SQL,可以给你一个想法/方向
#standardSQL
CREATE TEMP FUNCTION get_transactions(t ANY TYPE) AS (
IFNULL(CAST(REGEXP_EXTRACT(TO_JSON_STRING(t), r'"transactions":(\d+)') AS INT64), 0)
);
WITH `project.dataset.table1` AS (
SELECT 1 id, 2 col1, 3 col2, 4 col3, 12 transactions
), `project.dataset.table2` AS (
SELECT 2 id, 3 col1, 4 col2, 5 col3
)
SELECT id, col1, col2, col3, get_transactions(t) AS transactions
FROM `project.dataset.table1` t
UNION ALL
SELECT id, col1, col2, col3, get_transactions(t) AS transactions
FROM `project.dataset.table2` t
同时,遗留SQL的有趣特性允许您使用COALESCE进行操作,如下面的示例所示
#legacySQL
SELECT id, col1, col2, col3, COALESCE(transactions,0) AS transactions
FROM [project:dataset.table1], [project:dataset.table2]
注意:旧SQL中表之间的逗号表示UNION ALL
如果要模拟相同的采样数据
#legacySQL
SELECT id, col1, col2, col3, COALESCE(transactions,0) AS transactions
FROM (
SELECT 1 id, 2 col1, 3 col2, 4 col3, 12 transactions
), (
SELECT 2 id, 3 col1, 4 col2, 5 col3
)
结果将与standardSQL示例中的结果相同
Row id col1 col2 col3 transactions
1 1 2 3 4 12
2 2 3 4 5 0
下面是BigQuery标准SQL,可以给你一个想法/方向
#standardSQL
CREATE TEMP FUNCTION get_transactions(t ANY TYPE) AS (
IFNULL(CAST(REGEXP_EXTRACT(TO_JSON_STRING(t), r'"transactions":(\d+)') AS INT64), 0)
);
WITH `project.dataset.table1` AS (
SELECT 1 id, 2 col1, 3 col2, 4 col3, 12 transactions
), `project.dataset.table2` AS (
SELECT 2 id, 3 col1, 4 col2, 5 col3
)
SELECT id, col1, col2, col3, get_transactions(t) AS transactions
FROM `project.dataset.table1` t
UNION ALL
SELECT id, col1, col2, col3, get_transactions(t) AS transactions
FROM `project.dataset.table2` t
同时,遗留SQL的有趣特性允许您使用COALESCE进行操作,如下面的示例所示
#legacySQL
SELECT id, col1, col2, col3, COALESCE(transactions,0) AS transactions
FROM [project:dataset.table1], [project:dataset.table2]
注意:旧SQL中表之间的逗号表示UNION ALL
如果要模拟相同的采样数据
#legacySQL
SELECT id, col1, col2, col3, COALESCE(transactions,0) AS transactions
FROM (
SELECT 1 id, 2 col1, 3 col2, 4 col3, 12 transactions
), (
SELECT 2 id, 3 col1, 4 col2, 5 col3
)
结果将与standardSQL示例中的结果相同
Row id col1 col2 col3 transactions
1 1 2 3 4 12
2 2 3 4 5 0
谢谢-alias works不知道该函数,但仍然获得类似的问题名称。在tWell中未找到事务。您的表是否有名为transactions的列?谢谢-alias works不知道该函数,但仍然获得类似的问题名称。在tWell中未找到事务。您的表是否有名为transactions的列交易?