繁体   English   中英

有没有办法提取列值并在 where 子句中使用它?

[英]Is there a way to extract a column value and use it in a where clause?

所以我有这张表,我们称它为表 A。表 A 包含一列,其中包含一段 sql 代码。 例如,它可能有一个字符串,表示“location = 'Paris'”。 我的问题是尝试使用 where 语句提取此值并在 where 子句中使用它。

select *
from Table B
where (select location from table A) and day='Monday'

此代码段的问题在于返回的值是字符串而不是布尔值,并且 BQ 不允许字符串和布尔值使用 and 运算符。

所需的结果将返回当天为星期一且位置为巴黎的所有行

PS这是在BigQuery中

考虑以下示例作为如何实现它的示例

execute immediate '''
select *
from tableB t
where ''' || (select location from tableA)   

您可以使用以下脚本与简化/虚拟数据进行测试

create temp table tableA as (
  select "location = 'Paris'" location
);
create temp table tableB as (
  select 1 id, 'LA' location union all 
  select 2, 'NY' union all 
  select 3, 'Paris'
);
execute immediate '''
select *
from tableB t
where ''' || (select location from tableA)    

具有以下输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM