sPujade
sPujade

Reputation: 93

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

This is my query: How to convert it into standard SQL please? Thank you so much for your help. I don't know of to do it.

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 

I try do translate it into standard sql but I got the following error:

Syntax error: Unexpected floating point literal "85801771."

Maybe there are others mistakes in my query.

standard 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  

Upvotes: 0

Views: 1827

Answers (1)

Elliott Brossard
Elliott Brossard

Reputation: 33745

Instead of rewriting your query for you, it would probably be more useful to you in the long run to talk about some of the differences between legacy and standard SQL and point you toward documentation. Stepping through the parts of your query:

  • SEC_TO_TIMESTAMP is equivalent to TIMESTAMP_SECONDS.
  • TIME (with an INT64 in micros as input) is equivalent to FORMAT_TIMESTAMP('%H:%M:%S', TIMESTAMP_MICROS(micros)).
  • There is no STRING function, but you can use CAST(expr AS STRING).
  • FLATTEN is not a function in standard SQL. Instead, perform a CROSS JOIN with the array.
  • CONTAINS is not a function in standard SQL, but you can use LIKE '%website.fr%'.

Many of these differences are covered in the 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.

Upvotes: 3

Related Questions