Big Query:将遗留 sql 请求转换为标准 SQL 请求?

Big Query: converting a legacy sql request to a standard SQL request?

这是我的查询:如何将其转换为标准 SQL?非常感谢你的帮助。我不知道该怎么做。

SELECT date,
    max(case when customDimensions.index = 1 then customDimensions.value end) AS CUSTOMDIMENSIONS_VALUE, 
    visitNumber,
    fullvisitorid,
    visitStartTime,
    SEC_TO_TIMESTAMP(visitStartTime) AS humain,
    TIME (visitStartTime+3600 ) AS Paris_timezone,
    hits.hour,
    hits.minute,
    CONCAT(fullvisitorid, STRING(visitid)) AS sessionid,
    max(CASE WHEN hits.customDimensions.index = 11 THEN hits.customDimensions.value END) AS localproductname,
    device.deviceCategory,
    hits.page.pagePath,
    IFNULL(hits.page.pagePathLevel2, '') AS HITS_PAGE_PAGEPATHLEVEL2,
    IFNULL(hits.page.pagePathLevel3, '') AS HITS_PAGE_PAGEPATHLEVEL3,
    MAX(CASE WHEN hits.customDimensions.index = 14 THEN hits.customDimensions.value END) AS assetpurpose,
    hits.hitNumber,
FROM (FLATTEN([85801771.ga_sessions_20161025], customDimensions.value )),
  (FLATTEN([85801771.ga_sessions_20161026], customDimensions.value )),
WHERE customDimensions.value != "null" AND customDimensions.value = "968a9587-0614-4155-9597-bf17aef42125" AND hits.type = 'PAGE' AND (customDimensions.index = 1 OR hits.customDimensions.index = 11 OR hits.customDimensions.index = 14
    OR hits.customDimensions.index = 27 ) AND hits.page.hostname CONTAINS 'website.fr' AND hits.type = 'PAGE'
GROUP EACH BY DATE, visitStartTime, humain, Paris_timezone, hits.hour, hits.minute, fullVisitorId, sessionid, visitNumber, device.deviceCategory, hits.page.pagePath, HITS_PAGE_PAGEPATHLEVEL2, HITS_PAGE_PAGEPATHLEVEL3, hits.hitNumber,
LIMIT 100000 

我尝试将其翻译成标准 sql 但我收到以下错误:

Syntax error: Unexpected floating point literal "85801771."

可能我的查询还有其他错误。

标准 SQL:

SELECT date,
    max(case when customDimensions.index = 1 then customDimensions.value end) AS CUSTOMDIMENSIONS_VALUE, 
    visitNumber,
    fullvisitorid,
    visitStartTime,
    SEC_TO_TIMESTAMP(visitStartTime) AS humain,
    TIME (visitStartTime+3600 ) AS Paris_timezone,
    hits.hour,
    hits.minute,
    CONCAT(fullvisitorid, STRING(visitid)) AS sessionid,
    max(CASE WHEN hits.customDimensions.index = 11 THEN hits.customDimensions.value END) AS localproductname,
    device.deviceCategory,
    hits.page.pagePath,
    IFNULL(hits.page.pagePathLevel2, '') AS HITS_PAGE_PAGEPATHLEVEL2,
    IFNULL(hits.page.pagePathLevel3, '') AS HITS_PAGE_PAGEPATHLEVEL3,
    MAX(CASE WHEN hits.customDimensions.index = 14 THEN hits.customDimensions.value END) AS assetpurpose,
    hits.hitNumber,
FROM (FLATTEN([85801771.ga_sessions_20161025], customDimensions.value )),
  (FLATTEN([85801771.ga_sessions_20161026], customDimensions.value )),
WHERE customDimensions.value != "null" AND customDimensions.value = "968a9587-0614-4155-9597-bf17aef42125" AND hits.type = 'PAGE' AND (customDimensions.index = 1 OR hits.customDimensions.index = 11 OR hits.customDimensions.index = 14
    OR hits.customDimensions.index = 27 ) AND hits.page.hostname CONTAINS 'website.fr' AND hits.type = 'PAGE'
GROUP BY DATE, visitStartTime, humain, Paris_timezone, hits.hour, hits.minute, fullVisitorId, sessionid, visitNumber, device.deviceCategory, hits.page.pagePath, HITS_PAGE_PAGEPATHLEVEL2, HITS_PAGE_PAGEPATHLEVEL3, hits.hitNumber,
LIMIT 100000  

与其为您重写您的查询,不如在冗长的 运行 中讨论遗留和标准 SQL 之间的一些差异并为您指明文档对您更有用。逐步完成查询的各个部分:

  • SEC_TO_TIMESTAMP 等同于 TIMESTAMP_SECONDS.
  • TIME(以微为单位的 INT64 作为输入)等同于 FORMAT_TIMESTAMP('%H:%M:%S', TIMESTAMP_MICROS(micros)).
  • 没有STRING功能,但可以使用CAST(expr AS STRING)
  • FLATTEN 不是标准 SQL 中的函数。相反,执行 CROSS JOIN with the array.
  • CONTAINS 不是标准 SQL 中的函数,但您可以使用 LIKE '%website.fr%'.

migration guide, which is a good starting point if you want to figure out how to translate a function or operator from legacy to standard SQL. You can read about the functions that I mentioned above in the functions and operators documentation.

涵盖了其中的许多差异