In BigQuery, I can successfully run the following query using standard SQL:
SELECT
COUNT(*) AS totalCount,
city,
DATE_TRUNC(timeInterval.intervalStart, YEAR) AS start
FROM
sandbox.CountByCity
GROUP BY
city, start
But it fails when I nest the start
value in a STRUCT, like this...
SELECT
COUNT(*) AS totalCount,
city,
STRUCT(
DATE_TRUNC(timeInterval.intervalStart, YEAR) AS start
) as timeSpan
FROM
sandbox.CountByCity
GROUP BY
city, timeSpan.start
In this case, I get the following error message:
Cannot GROUP BY field references from SELECT list alias timeSpan at [10:11]
What is the correct way to write the query so that the start
value is nested within a STRUCT?