大查询:将遗留sql请求转换为标准SQL请求?

时间:2017-03-21 09:03:05

标签: sql error-handling google-bigquery standard-sql

这是我的查询:如何将其转换为标准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,但我收到以下错误:

  

语法错误:意外的浮点文字“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  

1 个答案:

答案 0 :(得分:2)

与其重写您的查询,从长远来看,您可能更有用的是讨论遗留SQL和标准SQL之间的一些差异,并指出您的文档。单步执行查询的各个部分:

  • SEC_TO_TIMESTAMP相当于TIMESTAMP_SECONDS
  • TIME(使用micro64中的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中涵盖了许多这些差异,如果您想弄清楚如何将函数或运算符从遗留SQL转换为标准SQL,这是一个很好的起点。您可以在functions and operators documentation中了解我上面提到的功能。