我在Hive表中有两个列,即dt和hr,我想在两个给定的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)。
发布于 2019-01-22 11:05:40
您必须检查由or组合的3个条件。
如果dt是'2019-01-10',那么hr必须是>= '05'。
如果dt是'2019-01-15',那么hr必须是<= '15'。
用于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-22 10:27:34
只需为那些没有人力资源限制的日子添加一个条件:
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-22 10:41:00
为什么不添加一个新列并加入dt和hr
更新#选项卡集datetime1=强制转换(CONCAT(date1,‘',time1)为DATETIME2(7))
稍后,您可以从这个新添加的列中选择日期时间范围。
如果不想向表中添加任何新列,请将表中的值插入到临时表中,并加入该临时表中的日期时间。
https://stackoverflow.com/questions/54306004
复制相似问题