0

请您帮我解答一下 Bigquery 的标准 SQL 语法问题,好吗?我试图理解为什么(以及如何解决它)这个标准 SQL 查询返回 2 行,而这个 legacysql 一个只返回 1 行(我希望只有一个)。

标准 SQL

  SELECT 
  hits2.transaction.transactionId as transactionId
  FROM `ga-export-TTTT.1234567890.ga_sessions_*`
  ,UNNEST (hits) as hits2

WHERE 
hits2.transaction.transactionId = '03971163'

旧版 SQL

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;

将不胜感激任何帮助。

4

2 回答 2

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数组时,您会在结果中为每个数组元素获得一行。如果您希望与表中的行一一对应,您可以使用数组子查询(如上面的查询)在应用过滤器后“重新打包”数组元素。作为另一个示例,您可以使用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_*`;

transaction这将返回一个包含所有匹配条件的字段的数组transaction.transactionId = '03971163'。如果您只想要数组的单个元素,则可以在选择列表中使用带有 a 的子查询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_*`;
于 2017-02-23T16:32:59.397 回答
0

我看到的两个查询(对于旧版和标准 SQL 版本)的唯一区别是您可能会查询不同的表集!

ga-export-TTTT.1234567890在旧版 SQL 版本中 - 您将表列表限制为从 TIMESTAMP('2016-09-01') 到 TIMESTAMP('2017-02-14') 的时间段,而在标准 SQL 中,您查询数据集中 的所有 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'
于 2017-02-23T18:03:34.433 回答