如何基于默认值跳过特定列上的联接条件?

gjmwrych  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(379)

我有一个例子,我必须根据8列将表1与表2连接起来--段\层次\级别\名称、源\系统、段\代码、rtm \分销\渠道、交易\销售\类型、源\客户\国家\交易、客户\段
问题在表2中,对于一些随机行,这些列可以有一个默认值“all”,这意味着每当值为“all”时,我必须跳过加入该列。
样品记录:

我有一个解决方案,我可以根据一列的值为'all'的条件创建表2的多个示例,但在这种情况下,我必须创建很多示例,因为我的列数是8。
我正在寻找一个简单的解决这个问题的Hive。谢谢!

uttx8gqw

uttx8gqw1#

对于中的每一列,只需要1个join和 Table2 可能包含值的 'ALL' 天气状况 ON 条款应为:

... AND (Table1.columnName = Table2.columnName OR Table2.columnName = 'ALL') AND ...

而不仅仅是:

Table1.columnName = Table2.columnName

如果Hive不支持 ORON 条款,你可以用 CROSS JOIN 和一个 WHERE 条款:

SELECT ...
FROM Table1 CROSS JOIN Table2
WHERE (Table1.column1 = Table2.column1 OR Table2.column1 = 'ALL') 
   AND ...
e3bfsja2

e3bfsja22#

可以将连接条件表示为:

from table1 t1 join
     table2 t2
     on (t2.Segment_Hierarchy_Level_1_Name = t1.Segment_Hierarchy_Level_1_Name or
         t2.Segment_Hierarchy_Level_1_Name = 'ALL'
        ) and
        (t2.Source_system = t1.Source_system or
         t2.Source_system = 'ALL'
        ) and
        (t2.Segment_Code = t1.Segment_Code or
         t2.Segment_Code = 'ALL'
        ) and
        . . .   -- repeat for remaining columns

不过,我怀疑表演会很糟糕。您似乎有一些列不包含 'ALL' . 你应该把那些删掉,然后用这个短语 JOIN 作为:

from table1 t1 join
     table2 t2
     on t2.Segment_Hierarchy_Level_1_Name = t1.Segment_Hierarchy_Level_1_Name and
        t2.Source_system = t1.Source_system and
        t2.RTM_Distribution_Channel = t1.RTM_Distribution_Channel and
        . . .  - non-wildcarded columns
        (t2.Segment_Code = t1.Segment_Code or
         t2.Segment_Code = 'ALL'
        ) and
        . . .   -- repeat for remaining wildcarded columns

初始连接条件应该对性能有所帮助。
编辑:
可以使用 where 对于 OR 条件:

from table1 t1 join
     table2 t2
     on t2.Segment_Hierarchy_Level_1_Name = t1.Segment_Hierarchy_Level_1_Name and
        t2.Source_system = t1.Source_system and
        t2.RTM_Distribution_Channel = t1.RTM_Distribution_Channel and
        . . .  - non-wildcarded columns
where (t2.Segment_Code = t1.Segment_Code or
        t2.Segment_Code = 'ALL'
       ) and
        . . .   -- repeat for remaining wildcarded columns

也就是说,我认为最近版本的hive确实支持 OR .

相关问题