通过引用引用表将数据移动到其他表时替换列的值

时间:2018-01-15 14:46:26

标签: sql hive hiveql

我们有一张名为' Table1'列和值如下所示

---------------------------
action | component | type |
---------------------------
  1    |     2    |  1    |
  2    |     3    |  3    |
  3    |     4    |  2    |
---------------------------

和'表2'具有与" table1'。

完全相同的结构

现在我有另一张表'参考'如下

---------------------------
description | id  | value  |
---------------------------
   action   |  2  |  create|
   action   |  1  |  delete|
   action   |  3  |  update|
  component |  2  |  c1    |
  component |  4  |  c2    |
  component |  3  |  c3    |
   type     |  2  |  t1    |
   type     |  1  |  t2    |
   type     |  3  |  t3    |
---------------------------

现在,我需要从' table1'移动数据。到表2'通过参考'参考'表。我得到的表格如下所示。

action | component | type |
---------------------------
delete |     c1    |  t2  |
create |     c3    |  t3  |
update |     c2    |  t1  |
---------------------------

请帮我查询一下。提前谢谢。

2 个答案:

答案 0 :(得分:2)

您正在寻找多个联接:

select t1a.value as action,
       t1c.value as component,
       t1t.value as type
from table2 t2 join
     table1 t1a
     on t2.action = t1a.id and t1a.description = 'action' join
     table1 t1c
     on t2.component = t1c.id and t1c.description = 'component' join
     table1 t1t
     on t2.type = t1t.id and t1t.description = 'type';

答案 1 :(得分:1)

你可以这样做:

select 
    (select b.description from reference b where A.action = b.id and b.description = 'action') as action,
    (select c.description from reference c where A.action = c.id and c.description = 'component') as component,
    (select d.description from reference d where A.action = d.id and d.description = 'type') as type
from table_1 A