【问题标题】:SQL script within subquery (Big Query)?子查询(大查询)中的 SQL 脚本?
【发布时间】:2021-05-02 13:57:44
【问题描述】:

有没有办法在子查询中包含 SQL 脚本?

例如:

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

在我的情况下,不可能将变量声明移到子查询之外。 谢谢!

【问题讨论】:

    标签: sql google-bigquery subquery sql-scripts


    【解决方案1】:

    对于这个特定示例,您可以使用 CTE 来定义参数值:

    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;
    

    【讨论】:

    • 是的,这是我的第一个,但如果 my_table 很大(实际上确实如此),则交叉连接的性能会很差,因为它会复制 mytable 中每一行的参数。我想通过使用变量来解决这个问题。
    • @Yairh 。 . .交叉连接应该很简单,因为只有一行。
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多