在配置单元查询的SELECT部分​​中重用别名

时间:2018-04-05 19:36:34

标签: hadoop hive hdfs

我想在配置单元查询的SELECT部分​​中重用别名。我的用例是重用case语句的结果,但我相信这个例子就足够了:

create table testme as select 
  accountid as abc,
  abc as xyz
from account_attributes;

Hive抱怨无法找到'abc'。

任何建议的解决方法?

3 个答案:

答案 0 :(得分:0)

试试这个:

create table testme as select 
  accountid as abc,
  accountid as xyz
from account_attributes;

答案 1 :(得分:0)

你可以使用像这样的子查询来做到这一点

create table testme as  
select abc, abc as xyz from (
  accountid as abc
from account_attributes
) t;

答案 2 :(得分:0)

您只能在上部子查询中重新选择表达式的别名。

select abc, 
       abc as something,
       abc+account_id as abc_plus_accountid,
       accountid
   from
(select accountid as abc,
        accountid 
  from account_attributes )s
;

您也可以使用宏:

create temporary macro abc(id int) case when ... then ... end;

并在查询中使用它:

select abc(accountid) as abc, 
       abc(another_id) as abc2
 from table;

注意,宏不是一个函数,Hive用一个正文替换它的名字,在执行之前每次在查询中出现时替换参数。在上面的宏示例中,(id int)是参数,而不是列名,因此您可以使用不同的参数调用它:abc(accountid) as abcabc(another_id) as abc2。请在此处阅读重要版本信息:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Create/DropMacro