Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Using query fields for other fields in BigQuery

Hello, I have a question that I can't solve,
How do you do in bigquery to store the value of a subquery and use it in another field?

For example let's say that I have a subquery that returns me the number of passengers of a route. Then I want to do a compariso with that value for another field. The following code is correct for MySQL, how can I do the same procedure for BigQuery?

(SELECT @aux := COUNT(*) FROM consumptions WHERE route = "X1"),
IF(@aux = 0 ,@aux:= another_field, @auxpax) AS total_consumptions

I have tried several things such as using DECLARE/SET, ...

Help would be really appreciated 🙂

0 2 191
2 REPLIES 2

Can you give sample data, schema and your desired output for this scenario?

RC1
Bronze 4
Bronze 4

@i2afaur 
Not the optimal one but should do the job for you.

Can you try once 

select 
case when (SELECT  COUNT(*) FROM consumptions WHERE route = "X1")=0 then
field1
else
field2
end
from table1