Question

I'm trying to run the following query on Google BigQuery:

SELECT SUM(var1) AS Revenue
FROM [table1]
WHERE timeStamp = (SELECT MAX(timeStamp) FROM [table1])

I'm getting the following error:

Error: Encountered "" at line 3, column 19. Was expecting one of:

Is this not supported in BigQuery? If so, would there be an elegant alternative?

Was it helpful?

Solution

Subselect in a comparison predicate is not supported, but you can use IN.

SELECT SUM(var1) AS Revenue
FROM [table1]
WHERE timeStamp IN (SELECT MAX(timeStamp) FROM [table1])

OTHER TIPS

I would use Rank() to get the max timestamp, and filter the #1s in the where clause.

select SUM(var1) AS Revenue
From
(SELECT var1 
,RANK() OVER (ORDER BY timestamp DESC) as RNK
FROM [table1]
)
where RNK=1

I don't know how it works with BQ, but in other DB technologies it would be more efficient as it involves only single table scan rather than 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top