Whats the problem
I have a very large list of results. I want to filter my query to only include a small sub-set of those based on the most popular results within the table.
When I put my filtering subquery in the WITH
clause it doesn't work. But if I put it directly in the WHERE clause it does work. Why?
I'd prefer the first for clarity, but I can't get it to work.
I'm working in BigQuery with StandardSQL.
Example
WITH
most_common AS (
SELECT
page
FROM
`mydataset`
LIMIT
100
)
SELECT
*
FROM
`mydataset`
WHERE
page IN most_common
Here I'm trying to get all the results, where the page is in the first 100 rows.
It returns the following error:
- Syntax error: Expected "(" or keyword UNNEST but got identifier "most_common" at [12:12]
However if I put the subquery directly in the where it works fine.
SELECT
*
FROM
`mydataset`
WHERE
page IN (
SELECT
page
FROM
`mydataset`
LIMIT
100
)
My limited understanding
It says it wants unnest, but unnest turns an array to a table result as a sub-query this should already be a table.