【问题标题】:Reusing an alias in the SELECT portion of a hive query在配置单元查询的 SELECT 部分重用别名
【发布时间】:2023-03-08 16:47:01
【问题描述】:

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

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

Hive 抱怨找不到“abc”。

有什么建议的解决方法吗?

【问题讨论】:

  • 改用 accountid。别名不能用于同一级别。
  • 重点是我需要重用case语句的结果——而不仅仅是一个变量。

标签: hadoop hive hdfs


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您只能在上层子查询中重复使用 select 中的表达式别名。

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-18
        • 1970-01-01
        相关资源
        最近更新 更多