-1

我在 Hive 表中有两列dthr我想在两个给定的 dt 和 hr 值之间获取 dt 和 hr

例如: dt='2019-01-10' 和 '2019-01-15' 所以我想获得 dt 范围所以我将询问select * from table_name where dt >='2019-01-10' and dt<='2019-01-15';如何通过另外一列实现相同的功能,hr如下所示:

select * from table_name where (dt >='2019-01-10' and hr >='05') and (dt<='2019- 01-15' and hr <='15');

但是上面的查询没有按预期工作,它为所有日期返回 hr>='05' 但我想要 2019-01-10 和 2019-01-15 之间日期的所有 hr(00 到 23)

4

3 回答 3

1

您必须检查由 组合而成的 3 个条件or
如果dt'2019-01-10'那么hr一定是>= '05'
如果dt'2019-01-15'那么hr一定是<= '15'。对于(exclusive) 和(exclusive)之间
的任何其他 值,不应检查 的值。dt'2019-01-10''2019-01-15'
hr

select * 
from table_name 
where 
  (dt ='2019-01-10' and hr >= '05') 
  or
  (dt ='2019-01-15' and hr <= '15')
  or
  (dt > '2019-01-10' and dt < '2019-01-15')

替代解决方案:

select * 
from table_name 
where 
  concat(dt, ' ', hr) >= concat('2019-01-10', ' ', '05') 
  and
  concat(dt, ' ', hr) <= concat('2019-01-15', ' ', '15') 

如果你能使用between它就更好了:

select * 
from table_name 
where 
  concat(dt, ' ', hr) between 
  concat('2019-01-10', ' ', '05') and concat('2019-01-15', ' ', '15') 
于 2019-01-22T11:05:40.863 回答
0

为什么不添加一个新列并加入 dt 和 hr

update #tab set datetime1= CAST(CONCAT(date1, ' ', time1) AS DATETIME2(7))

稍后您可以从这个新添加的列中选择日期时间范围。

如果您不想向表中添加任何新列,请将表中的值插入到临时表中,然后在该临时表中加入日期时间。

于 2019-01-22T10:41:00.380 回答
0

只需为那些没有小时限制的日子添加一个条件:

select * from table_name
where ((dt >= '2019-01-10' and hr >= '05') and (dt <= '2019-01-15' and hr <='15'))
   or (dt > '2019-01-10' and dt < '2019-01-15')

或者,Hive 是否支持“行和表构造函数”:

select * from table_name
where (dt, hr) >= ('2019-01-10', '05') and (dt, hr) <= ('2019-01-15', '15')
于 2019-01-22T10:27:34.517 回答