如何在where子句中使用select查询的结果

0sgqnhkj  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(457)

来自teradata,我通常会创建一个包含一些变量的volatile表,这些变量将在我的代码中使用。
例如。,

create volatile table var as (
select 'filter_value' as var_field
) with data on commit preserve rows;

然后在select where子句中使用该表:

select * from table
where some_field = (select var_field from var);

我尝试在色调( Impala 编辑器)中执行类似的操作,但是出现了一个错误:

create table var as
select 'filter_value' as var_field

select * from table
    where some_field = (select var_field from var)

analysisexception:第5行出现语法错误:未定义:从表名隐藏^遇到:从预期值:case、cast、default、exists、false、if、interval、not、null、replace、truncate、true、identifier导致:异常:语法错误
有人知道如何做到这一点或复制色调这个特点?
不必在整个代码中定义我的变量,并将它们放在一个表的顶部,这样做很方便。

sbdsn5lh

sbdsn5lh1#

我有个解决办法。我试过了,我能完成你的目标。
teradata中使用的查询:
查询1:

create volatile table var as (
select 'filter_value' as var_field
) with data on commit preserve rows;

问题2:

select * from table
where some_field = (select var_field from var);

在impala中工作的查询与上述场景相同:
查询1:

create table var as
select 'filter_value' as var_field;

问题2:

SELECT * FROM test
where test_field in (select var_field from var);

测试步骤:
1.可变表创建:

create table var as
select 'filter_value' as var_field;

+-------------------+
| summary           |
+-------------------+
| Inserted 1 row(s) |
+-------------------+

2.易失性表数据检查:

SELECT * FROM var;

+--------------+
| var_field    |
+--------------+
| filter_value |
+--------------+

3.示例表创建和数据插入:

CREATE TABLE test 
(test_field string);

Fetched 0 row(s) in 0.81s

----

INSERT INTO test
values ("filter_value");

Modified 1 row(s) in 5.64s

----

INSERT INTO test
values ("filter_value2");

Modified 1 row(s) in 0.32s
----

4.样表数据检查:

select * from test;

+---------------+
| test_field    |
+---------------+
| filter_value  |
| filter_value2 |
+---------------+

5.目标:

SELECT * FROM test
where test_field in (select var_field from var);

+--------------+
| test_field   |
+--------------+
| filter_value |
+--------------+

相关问题