【问题标题】:Creating a New Hive Table with a Conditional Column Based off of Another table使用基于另一个表的条件列创建新 Hive 表
【发布时间】:2017-05-25 01:38:56
【问题描述】:

我正在尝试基于现有表创建一个新的 Hive 表。我需要一个 if 语句的帮助,它允许我从原始表中的 2 个单独的列创建一个合并列。列 new_name & old_item 和 uh_new_name 和 uh_old_item 具有相同的数据,我想要一个列。我无法更改表格,因为它是 Hive。作为最终结果,我需要 id、open_time,然后是列的组合。

这是我创建表格的代码:

CREATE TABLE CIs
  AS SELECT id, open_time, new_name, uh_new_name, old_item, uh_old_item from sm
        where open_time like '2016%' or open_time like '2017%'
        order by open_time desc;

这是我需要的组合列背后的一般逻辑:

If id like 'AB%' then old_item else new_item as combo_code
IF id like 'AB%' then uh_old_item else uh_new_item as combo_name

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    这只需要一个简单的 case 语句作为您的选择的一部分:

    case when id like 'AB%' then old_item else new_item end as combo_code,
    case when id like 'AB%' then uh_old_item else uh_new_item end as combo_name
    

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 2023-03-25
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多