如何在bigquery where子句中使用cte

问题描述 投票:0回答:1

我有一个查询,它采用 cte 的值进行过滤。

with cte1 as (
  select 1 as id
)

select 
  count(*)
from dataset.table1
where
 id= (select id from cte1 limit 1)

在上述情况下查询不起作用。但如果我硬编码

where id=1
,它就可以工作。我在这里缺少什么?

google-bigquery common-table-expression
1个回答
0
投票

给定的示例应该可以工作。但是,如果

limit 1
表上有多个条目,则
cte1
可能会导致一些问题。

with cte1 as (
  select 1 as id
  union all select 2
),
example as  (Select id from unnest([1,1,2,2,2,3,3,3,4]) as id)

select 
  #count(*)
  *
from example # dataset.table1
where
 #id= (select id from cte1 limit 1)
  id in (select distinct id from cte1 )
© www.soinside.com 2019 - 2024. All rights reserved.