sql - 如何根据默认值跳过某些列的连接条件?
问题描述
我有一个案例,我必须基于 8 列将 TABLE1 与 TABLE2 连接起来 ——Segment_Hierarchy_Level_1_Name、Source_system、Segment_Code、RTM_Distribution_Channel、Transaction_Sales_Type、Source_of_Customer、Multi_country_Deal、Customer_segment
对于某些随机行,问题在表 2 中,这些列的默认值为“ALL”,这意味着只要值为“ALL”,我就必须跳过加入该列。
样本记录:
我有一个解决方案,我可以根据列的值为“ALL”的条件创建表 2 的多个实例,但在这种情况下我必须创建很多实例,因为我的列数是 8。
我正在为 HIVE 中的这个问题寻找一个简单的解决方案。谢谢!
解决方案
您可以将连接条件表示为:
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
forOR
条件改写最后一个查询:
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
.
推荐阅读
- python - 使用 Apache 自动化烧瓶服务器。空气流动
- angular - 如何为 Angular 包含 ngx-formly 的时区?
- python - Browser.get() 无法使用 python 使用 Selenium
- animation - 如何快速放大触摸项目的规模
- flutter - 当我安装了 2 个版本的颤振时,如何使用特定版本的颤振来构建 apk?
- sql - 行总和作为列
- jquery - 无法从我的 jquery Ajax 调用中更新输入文本框
- c++ - Qt C++ 将图像保存到指定文件夹
- mysql - Python3比较来自DB的数据并将答案写回DB
- http - fasthttp 简单示例的高延迟峰值