如何在 Hive SQL 中使用查询结果作为变量

问题描述

我无法访问 hive sql 中的 shell 包装器脚本,因此我需要帮助仅在没有 shell 的 Hive sql 脚本中修改以下 sql

下面两条sql语句都不起作用,我想要做的是从mytable中检索日期在curr_date和Last_date之间的数据,如何修改

set hivevar:curr_date = '2017-03-11';

set hivevar:Last_date = Select Max(dt) from tb_date;

select * from mytable where dt >= ${curr_date} and dt <= (${Last_date}); 

select * from mytable where dt between ${curr_date} and (${Last_date});

解决方法

无法为此使用变量。使用子查询:

set hivevar:curr_date='2017-03-11';

select t1.* 
  from 
      mytable t1 
      cross join (Select Max(dt) as max_dt from tb_date) dt
 where t1.dt >= ${curr_date} 
       and t1.dt <= dt.max_dt;