如何在WHERE子句中使用SELECT查询的结果

时间:2019-07-13 08:13:00

标签: hiveql impala hue

来自Teradata,我通常会创建一个易失性表,其中包含一些将在我的代码中使用的变量。

例如

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);

我正在尝试在HUE(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,   标识的原因:异常:语法错误

有人知道如何做或在Hue中复制此功能吗?

不必在整个代码中定义我的变量并将它们全部保留在一个表的顶部,这很方便。

1 个答案:

答案 0 :(得分:0)

我有解决办法。我在顺化(Hue)尝试过,就可以实现您的目标。

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 |
+--------------+