为什么 Bigquery 的 standardsql returns 多行而 legacysql 只有一行?
Why Bigquery's standardsql returns multiple rows while legacysql just one?
你能帮我解决一下 Bigquery 的标准 SQL 语法问题吗?
我试图理解为什么(以及如何修复它)这个标准的 sql 查询 returns 2 行,而这个 legacysql 一个 returns 只是 1(我希望只有一个)。
标准SQL
SELECT
hits2.transaction.transactionId as transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`
,UNNEST (hits) as hits2
WHERE
hits2.transaction.transactionId = '03971163'
LegacySQL
select
hits.transaction.transactionId
FROM
TABLE_DATE_RANGE([ga-export-TTTT:1234567890.ga_sessions_], TIMESTAMP('2016-09-01'), TIMESTAMP('2017-02-14'))
WHERE
hits.transaction.transactionId = '03971163'
阅读帮助后,我还尝试了这个 StandardSQL 一个,结果中有相同的两行:
select
title
from
(
SELECT
ARRAY(SELECT transaction.transactionId FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163') AS title
FROM `ga-export-TTTTT.1234567890.ga_sessions_*`
)
WHERE ARRAY_LENGTH(title) > 0;
在此方面如有任何帮助,我们将不胜感激。
试试这个:
#standardSQL
SELECT
ARRAY(SELECT transaction.transactionId
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163')
AS transactionIds
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
当您对数组使用 CROSS JOIN
时,您会在结果中为每个数组元素获得一行。如果你想与 table 中的行一一对应,你可以在应用过滤器后使用数组子查询(如上面的查询)到 "repackage" 数组元素。作为另一个示例,您可以将 ARRAY
子查询与 SELECT AS STRUCT
:
一起使用
#standardSQL
SELECT
ARRAY(SELECT AS STRUCT transaction.*
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163')
AS transactions
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
这将 return 一个包含 transaction
内所有字段的数组,用于匹配 transaction.transactionId = '03971163'
条件的命中。如果您只想要数组的单个元素,则可以在 select 列表中使用带有 LIMIT
的子查询:
#standardSQL
SELECT
(SELECT transaction.transactionId
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163'
LIMIT 1)
AS transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
或:
#standardSQL
SELECT
(SELECT transaction
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163'
LIMIT 1)
AS transaction
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
我看到你的两个查询(旧版和标准 SQL 版本)的唯一区别是你可能查询不同的表集!
在旧版 SQL 版本中 – 您将表列表限制在从 TIMESTAMP('2016-09-01') 到 TIMESTAMP('2017-02-14') 的时间段内,而在标准 SQL 您查询 ga-export-TTTT.1234567890
数据集
中的所有 ga_sessions_ 表
尝试在下面将表格过滤到与旧版相同的列表 SQL
#standardSQL
SELECT hits2.transaction.transactionId as transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`, UNNEST (hits) as hits2
WHERE hits2.transaction.transactionId = '03971163'
AND _TABLE_SUFFIX BETWEEN '20160901' AND '20170214'
你能帮我解决一下 Bigquery 的标准 SQL 语法问题吗? 我试图理解为什么(以及如何修复它)这个标准的 sql 查询 returns 2 行,而这个 legacysql 一个 returns 只是 1(我希望只有一个)。
标准SQL
SELECT
hits2.transaction.transactionId as transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`
,UNNEST (hits) as hits2
WHERE
hits2.transaction.transactionId = '03971163'
LegacySQL
select
hits.transaction.transactionId
FROM
TABLE_DATE_RANGE([ga-export-TTTT:1234567890.ga_sessions_], TIMESTAMP('2016-09-01'), TIMESTAMP('2017-02-14'))
WHERE
hits.transaction.transactionId = '03971163'
阅读帮助后,我还尝试了这个 StandardSQL 一个,结果中有相同的两行:
select
title
from
(
SELECT
ARRAY(SELECT transaction.transactionId FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163') AS title
FROM `ga-export-TTTTT.1234567890.ga_sessions_*`
)
WHERE ARRAY_LENGTH(title) > 0;
在此方面如有任何帮助,我们将不胜感激。
试试这个:
#standardSQL
SELECT
ARRAY(SELECT transaction.transactionId
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163')
AS transactionIds
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
当您对数组使用 CROSS JOIN
时,您会在结果中为每个数组元素获得一行。如果你想与 table 中的行一一对应,你可以在应用过滤器后使用数组子查询(如上面的查询)到 "repackage" 数组元素。作为另一个示例,您可以将 ARRAY
子查询与 SELECT AS STRUCT
:
#standardSQL
SELECT
ARRAY(SELECT AS STRUCT transaction.*
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163')
AS transactions
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
这将 return 一个包含 transaction
内所有字段的数组,用于匹配 transaction.transactionId = '03971163'
条件的命中。如果您只想要数组的单个元素,则可以在 select 列表中使用带有 LIMIT
的子查询:
#standardSQL
SELECT
(SELECT transaction.transactionId
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163'
LIMIT 1)
AS transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
或:
#standardSQL
SELECT
(SELECT transaction
FROM UNNEST(hits)
WHERE transaction.transactionId = '03971163'
LIMIT 1)
AS transaction
FROM `ga-export-TTTT.1234567890.ga_sessions_*`;
我看到你的两个查询(旧版和标准 SQL 版本)的唯一区别是你可能查询不同的表集!
在旧版 SQL 版本中 – 您将表列表限制在从 TIMESTAMP('2016-09-01') 到 TIMESTAMP('2017-02-14') 的时间段内,而在标准 SQL 您查询 ga-export-TTTT.1234567890
数据集
尝试在下面将表格过滤到与旧版相同的列表 SQL
#standardSQL
SELECT hits2.transaction.transactionId as transactionId
FROM `ga-export-TTTT.1234567890.ga_sessions_*`, UNNEST (hits) as hits2
WHERE hits2.transaction.transactionId = '03971163'
AND _TABLE_SUFFIX BETWEEN '20160901' AND '20170214'