Solved! Go to Solution.
The error message "Expected end of input but got keyword CROSS at [118:5]" indicates that there is a syntax issue in your SQL query, specifically around the use of the CROSS JOIN
clause. Let's analyze the structure of your query to identify and correct the problem.
From the structure of your query, it seems that the CROSS JOIN UNNEST(events) AS evt
is intended to flatten the array of events for each session. However, the placement of this clause might be causing the syntax error.
In BigQuery SQL, a CROSS JOIN
should be used within the FROM
clause of a SELECT
statement. It looks like your CROSS JOIN
is outside of any SELECT
statement, which is likely causing the syntax error.
To fix this, you need to ensure that the CROSS JOIN
is part of a SELECT
statement. Here's a revised version of your query with the CROSS JOIN
correctly placed:
CREATE TABLE `{prodid}.{datasetid}.{tableid}`
PARTITION BY DATE(event_timestamp)
CLUSTER BY metric_name
AS (
SELECT
CURRENT_TIMESTAMP() AS last_updated,
ga_session_id,
IF(EXISTS(SELECT 1 FROM UNNEST(events) AS e WHERE e.event_name = 'first_visit'), 'New user', 'Returning user') AS user_type,
IF((SELECT MAX(session_engaged) FROM UNNEST(events)) > 0, 'Engaged', 'Not engaged') AS session_engagement,
evt.* EXCEPT (session_engaged, event_name),
event_name AS metric_name,
CASE metric_rating
WHEN 'good' THEN 'Good'
WHEN 'ni' THEN 'Needs Improvement'
WHEN 'needs-improvement' THEN 'Needs Improvement'
WHEN 'poor' THEN 'Poor'
ELSE metric_rating
END AS metric_status
FROM (
SELECT
ga_session_id,
ARRAY_AGG(custom_event) AS events
FROM (
-- Your inner SELECT statements here
)
WHERE ga_session_id IS NOT NULL
GROUP BY ga_session_id
)
CROSS JOIN UNNEST(events) AS evt
WHERE evt.event_name NOT IN ('first_visit', 'purchase')
)
CROSS JOIN UNNEST(events) AS evt
is now correctly placed within the FROM
clause of the outermost SELECT
statement. This should resolve the syntax error you were encountering.The error message "Expected end of input but got keyword CROSS at [118:5]" indicates that there is a syntax issue in your SQL query, specifically around the use of the CROSS JOIN
clause. Let's analyze the structure of your query to identify and correct the problem.
From the structure of your query, it seems that the CROSS JOIN UNNEST(events) AS evt
is intended to flatten the array of events for each session. However, the placement of this clause might be causing the syntax error.
In BigQuery SQL, a CROSS JOIN
should be used within the FROM
clause of a SELECT
statement. It looks like your CROSS JOIN
is outside of any SELECT
statement, which is likely causing the syntax error.
To fix this, you need to ensure that the CROSS JOIN
is part of a SELECT
statement. Here's a revised version of your query with the CROSS JOIN
correctly placed:
CREATE TABLE `{prodid}.{datasetid}.{tableid}`
PARTITION BY DATE(event_timestamp)
CLUSTER BY metric_name
AS (
SELECT
CURRENT_TIMESTAMP() AS last_updated,
ga_session_id,
IF(EXISTS(SELECT 1 FROM UNNEST(events) AS e WHERE e.event_name = 'first_visit'), 'New user', 'Returning user') AS user_type,
IF((SELECT MAX(session_engaged) FROM UNNEST(events)) > 0, 'Engaged', 'Not engaged') AS session_engagement,
evt.* EXCEPT (session_engaged, event_name),
event_name AS metric_name,
CASE metric_rating
WHEN 'good' THEN 'Good'
WHEN 'ni' THEN 'Needs Improvement'
WHEN 'needs-improvement' THEN 'Needs Improvement'
WHEN 'poor' THEN 'Poor'
ELSE metric_rating
END AS metric_status
FROM (
SELECT
ga_session_id,
ARRAY_AGG(custom_event) AS events
FROM (
-- Your inner SELECT statements here
)
WHERE ga_session_id IS NOT NULL
GROUP BY ga_session_id
)
CROSS JOIN UNNEST(events) AS evt
WHERE evt.event_name NOT IN ('first_visit', 'purchase')
)
CROSS JOIN UNNEST(events) AS evt
is now correctly placed within the FROM
clause of the outermost SELECT
statement. This should resolve the syntax error you were encountering.Thanks very much for this! works wonderfully ❤️