I'm running a fairly complex set of nested aggregations in BQ and have run into the following issue:
If I ask for ROW_NUMBER() at the highest level of the query, BQ seems not to recognize my column names. If I ask for ROW_NUMBER() at the innermost level of the query, I magically get a row number.
Here's a (simplified) version of my query:
SELECT
ROW_NUMBER() OVER() row_id, // query fails with ROW_NUMBER() here
price,
store,
week,
f.product product,
COUNT(DISTINCT f.product) OVER (PARTITION BY store, week) product_count,
price_max-price_min weekly_price_range,
FROM (
SELECT
MIN(price) OVER (PARTITION BY store, week) price_min,
MAX(price) OVER (PARTITION BY store, week) price_max,
*
FROM (
SELECT
dollars / units price,
*
FROM store_sales_facts f
JOIN product_facts p
ON p.product = f.product
)
)
If I remove the request for ROW_NUMBER(), the query succeeds. If I include the ROW_NUMBER() function, BQ fails and says "Error: field 'price' not found"
I'm using the following as a workaround, but I'm curious if anyone can explain why this works while the above doesn't?
SELECT
row_id,
price,
store,
week,
f.product product,
COUNT(DISTINCT f.product) OVER (PARTITION BY store, week) product_count,
price_max-price_min weekly_price_range,
FROM (
SELECT
MIN(price) OVER (PARTITION BY store, week) price_min,
MAX(price) OVER (PARTITION BY store, week) price_max,
*
FROM (
SELECT
ROW_NUMBER() OVER() row_id, // query succeeds with ROW_NUMBER() here
dollars / units price,
*
FROM store_sales_facts f
JOIN product_facts p
ON p.product = f.product
)
)
ORDER BY row_id
I understand the ROW_NUMBER() window function was added relatively recently. Is this a quirk of the function itself? Any help would be appreciated.