Home > database >  Possible to pass an expression to a limit in BigQuery
Possible to pass an expression to a limit in BigQuery

Time:08-29

Is there any workaround to accomplish something like the following in BigQuery:

select 1 limit (select 1)
select 1 limit 2-1

In other words, pass an expression to the LIMIT keyword? Or, is there no possible way to emulate that behavior?

CodePudding user response:

Folowing the link provided, you can build it like this

DECLARE lim DEFAULT (SELECT 1);

EXECUTE IMMEDIATE """
 select 1 LIMIT ?
"""
USING  lim;

CodePudding user response:

Or, is there no possible way to emulate that behavior?

I would rather go with below approach

select * 
from your_table 
qualify row_number() over() < (select 15 - 2)      

or in more practical case - something like

select * 
from your_table 
qualify row_number() over() < (select limit_value from other_table limit 1 )
  • Related