使用基于另一个表的条件列创建新的Hive表

时间:2017-05-24 16:18:05

标签: sql hive

我正在尝试根据现有表创建一个新的Hive表。我需要一个if语句的帮助,这个语句允许我从原始表中的2个单独的列创建一个合并列。列new_name& old_item和uh_new_name以及uh_old_item具有相同的数据,我希望有一个列。我不能改变桌子,因为它的蜂巢。作为最终结果,我需要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

1 个答案:

答案 0 :(得分:0)

这只需要一个简单的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
相关问题