Skip to content
Advertisement

SQL script within subquery (Big Query)?

Is there a way to have SQL scripts within a subquery ?

For example:

select col_1, col_2, count(*) from (
Declare max_radius INT64;
set max_radius = 250;

select * from my_table
where radius <  max_radius
)
group by col_1,col_2

In my case it is not possible to move the variable declaration outside of the subquery. Thanks !

Advertisement

Answer

For this particular example, you could use a CTE to define the parameter values:

with params as (
      select 250 as max_radius
     )
select t.col_1, t.col_2, count(*)
from params p cross join
     my_table t
where t.radius < p.max_radius
group by t.col_1, t.col_2;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement