【问题标题】:Apache Hive - Merging a single column from one table into another under a new nameApache Hive - 以新名称将一个表中的单个列合并到另一个表中
【发布时间】:2020-11-16 21:40:10
【问题描述】:

我正在处理两个表,它们都有一个同名的 ID 列,我需要找到一种方法将一个表中的 id 合并到另一个表中,并使用一个新的列名。此外,我需要保留左表中的所有行,并使用右表中的现有 ID 更新新列。如果右表没有对应的ID,则合并列后的值为null。

Table 1

Houses
--------------------------------------
| ID |  Address                    
--------------------------------------
| 1  |  123 Main
| 2  |  234 Center
| 3  |  345 North Street

Table 2
Houses that are blue
--------------------------------------
| ID |  Address
--------------------------------------
| 2  |  234 Center



Resultant table:  
Houses
    
--------------------------------------
| ID |  Address          |  BlueHouseID
--------------------------------------
| 1  |  123 Main         |  NULL
| 2  |  234 Center       |  2
| 3  |  345 North        |  NULL

在此先感谢您提供有关设置此查询的任何帮助。这最终将进入一个覆盖文本文件以供以后提取。

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    只需left join

    select h.*, b.id as blue_house_id
    from houses h
    left join blue_houses b on b.id = h.id
    

    或者您可能想匹配地址而不是id

    select h.*, b.id as blue_house_id
    from houses h
    left join blue_houses b on b.address = h.address
    

    【讨论】:

      【解决方案2】:

      使用left join:

      select h.*, hb.id as blue_id
      from houses h left join
           houses_blue hb
           on h.id = hb.id
      

      【讨论】:

        猜你喜欢
        • 2020-06-14
        • 2019-07-27
        • 2021-10-06
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多