Я пытался преобразовать унаследованный код SQL BigQuery в стандартный SQL, но получаю массу ошибок.
Вот оригинальный Legacy SQL:
SELECT t.page_path,
t.second_page_path,
t.third_page_path,
t.fourth_page_path,
CONCAT(t.page_path,IF(t.second_page_path IS NULL,"","-"),
IFNULL(t.second_page_path,""),IF(t.third_page_path IS NULL,"","-"),
IFNULL(t.third_page_path,""),IF(t.fourth_page_path IS NULL,"","-"),
IFNULL(t.fourth_page_path,"")) AS full_page_journey,
count(sessionId) AS total_sessions
FROM (
SELECT
CONCAT(fullVisitorId,"-",STRING(visitStartTime)) AS sessionId,
hits.hitNumber,
hits.page.pagePath AS page_path,
LEAD(hits.page.pagePath) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS second_page_path,
LEAD(hits.page.pagePath,2) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS third_page_path,
LEAD(hits.page.pagePath,3) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS fourth_page_path
FROM
TABLE_DATE_RANGE( [xxxxxxx:xxxxxxx.ga_sessions_],
TIMESTAMP('2017-01-01'), TIMESTAMP('2017-01-02') )
WHERE
hits.type="PAGE"
) t
WHERE t.hits.hitNumber=1
GROUP BY t.page_path,
t.second_page_path,
t.third_page_path,
t.fourth_page_path,
full_page_journey
ORDER BY total_sessions DESC
ОБНОВЛЕНО (отредактировано): И вот что я смог сделать до сих пор:
SELECT t.page_path,
t.second_page_path,
t.third_page_path,
t.fourth_page_path,
CONCAT(t.page_path,IF(t.second_page_path IS NULL,"","-"),
IFNULL(t.second_page_path,""),IF(t.third_page_path IS NULL,"","-"),
IFNULL(t.third_page_path,""),IF(t.fourth_page_path IS NULL,"","-"),
IFNULL(t.fourth_page_path,"")) AS full_page_journey,
count(sessionId) AS total_sessions
FROM (
SELECT
CONCAT(fullVisitorId,"-",cast(visitStartTime as string)) AS sessionId,
hits.hitNumber,
hits.page.pagePath AS page_path,
LEAD(hits.page.pagePath) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS second_page_path,
LEAD(hits.page.pagePath,2) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS third_page_path,
LEAD(hits.page.pagePath,3) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS fourth_page_path
FROM
`xxxxxxxxxxx.xxxxxxx.ga_sessions_*`,
UNNEST(hits) AS hits
WHERE
_TABLE_SUFFIX BETWEEN
FORMAT_DATE('%Y%m%d', DATE_ADD(CURRENT_DATE(), INTERVAL -16 DAY))AND
FORMAT_DATE('%Y%m%d', DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY))AND
hits.type = 'PAGE' ) AS t
WHERE t.hits.hitNumber = 1
GROUP BY t.page_path,
t.second_page_path,
t.third_page_path,
t.fourth_page_path,
full_page_journey
ORDER BY total_sessions DESC
Этобудет здорово, если кто-то может помочь определить, что не так с синтаксисом.
Вот некоторые из полученных ошибок:
Невозможно получить доступ к значению hitNumber поля для значения с типом ARRAY
Проблемы с "_TABLE_SUFFIX", которые я прочитал, были связаны с подстановочным знаком.