【问题标题】:Comparison of column from a table with the columns of another table in hive将表中的列与 hive 中另一个表的列进行比较
【发布时间】:2018-05-17 12:37:15
【问题描述】:

我有两个配置单元表 table1 和 table2。两个表中没有相同的列。

表1:

------
| id |
------
| 22 |
| 11 |
| 56 |
| 15 |
------

表2:

-------------
| from | to |
-------------
|  10  | 20 |
|  21  | 35 |
|  40  | 60 |
-------------

table1的id列必须从table2的'from'和'to'列中检查它属于哪个范围。

预期输出:

------------------
| id | from | to |
------------------
| 22 |  21  | 35 |
| 11 |  10  | 20 |
| 56 |  40  | 60 |
| 15 |  10  | 20 |
------------------

尝试使用交叉连接和where 条件并能够获得所需的输出(但希望避免交叉连接)。还尝试使用“存在”命令但在获取输出时遇到错误:

查询:

select id from table1 t1
where exists(select 1 from table2 t2
where t1.id between t2.from and t2.to) ;

但出现错误:subquery expression refers to both parent and subquery expressions and is not a valid join condition

任何建议都会有帮助。

谢谢

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    这里是一步一步的解释,以获得你想要的结果:

    hive> create table table1(id int);
    hive> create table table2(from_ int, to_ int);
    hive> select * from table1;
    OK
    22
    11
    56
    15
    
    hive> select * from table2;
    OK
    10  20
    21  35
    40  60
    

    您的 SQL 应如下所示以获得所需的结果:

    select id,from_,to_
    from table1 t1
    left join table2 t2 on(t1.id between t2.from_ and t2.to_);
    
    Output:
    id      from_   to_
    22      21      35
    11      10      20
    56      40      60
    15      10      20
    

    【讨论】:

    • 我使用带有“_”的列后缀,因为“from”和“to”是引擎保留的键盘。希望你清楚。
    【解决方案2】:

    从 Hive 2.2.0 开始支持 ON 子句中的复杂表达式(请参阅HIVE-15211HIVE-15251)。在此之前,Hive 不支持非相等条件的连接条件。

    因此,Hive CROSS JOIN + 过滤器

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多