0

这是我的查询:请如何将其转换为标准 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  
4

1 回答 1

3

与其为您重写查询,从长远来看,讨论遗留 SQL 和标准 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使用数组执行 a 。
  • CONTAINS不是标准 SQL 中的函数,但您可以使用LIKE '%website.fr%'.

迁移指南中涵盖了其中许多差异,如果您想了解如何将函数或运算符从旧版 SQL 转换为标准 SQL,这是一个很好的起点。您可以在函数和操作符文档中阅读我上面提到的函数。

于 2017-03-21T10:05:43.933 回答