子句中的Hive子查询(从表1中选择*,其中dt>(从表2中选择max(dt)))..请建议替代方案

时间:2014-07-01 17:21:00

标签: hive hiveql apache-hive

我正在寻找像hive这样的东西

Select * from table 1 where dt > (Select max(dt) from table2) 

显然,hive不支持where子句中的子查询,即使我使用连接或半连接,它只比较=而不是> (据我所知)。

有人可以建议我在hive中编写相同查询的替代解决方案吗?

2 个答案:

答案 0 :(得分:5)

select table_1.* from table_1
join (select max(dt) as max_dt from table2) t2
where table_1.dt > t2.max_dt

你是对的,你只能在join on子句中拥有相等的条件,但你可以在where子句中拥有你想要的任何东西。

通常不建议这样做,因为没有on条款意味着Hive会先做一个完整的笛卡尔积,然后过滤,但由于连接的一边只有一行,所以&#39这不是问题。

答案 1 :(得分:1)

示例1:

Select * from table1 A
Inner join table2 B
On A.id =B.id 
And (A.name,A.Roll_no) not in (select name,roll_no from table2) 

示例2:

Select * from table A 
where (A.id,A.name,A.Roll_no) in(Select I'd,name,roll_no from table B ) 

如果我在where子句中使用两列或多列来获取结果子查询,那么Query不会运行pls建议我正确的解决方案。

相关问题