两个日期字段之间的时差

时间:2017-04-18 23:37:32

标签: hiveql

我有一个日期字段名称" dts"字符串格式。我想根据他们的时差(以小时为单位)找出所有记录。运行事件时间应大于或等于eat事件。

enter image description here

输出应为:

enter image description here

1 个答案:

答案 0 :(得分:1)

将时间戳转换为秒,然后减去,将结果除以3600得到小时,用例+计数按范围计算,如下所示:

select count(case when diff_hrs >24 then 1 end) as more_24,
       count(case when diff_hrs <=24 then 1 end) as less_than_24,
       ...
       count(case when diff_hrs >=2 and diff_hrs <=3 then 1 end) as hrs_2_to_3,
       ...
from
(
select
abs(unix_timestamp(dts) - unix_timestamp(dts-eat)))/60/60 as diff_hrs
from table
)s;