在蜂巢中获取时差

时间:2019-06-28 17:34:47

标签: hiveql

我有2个像hh:mm这样的时间字段,我需要弄清楚它的区别。 例如:11:55和11:25的区别应该是.30或30,我该如何实现

它采用AM / PM格式,例如11:55 AM和11:25 AM,我已使用from_unixtime函数更改为正常时间,但现在无法获得差异。

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

select (unix_timestamp(end_time, 'HH:mm') 
- unix_timestamp(start_time, 'HH:mm'))/60 from  temp.test_table; 

您应该将两个日期都转换为秒,然后减去它们。最后,如果要在数分钟内得到结果,请将其除以60。

代码示例:

create  table temp.test_table (                                                                                                                                                                                     
        start_time                  CHAR(5) 
        ,end_time                  CHAR(5) 
)
stored as parquet location '../temp.db/test_table'; 
INSERT INTO TABLE temp.test_table VALUES ('11:25', '11:55');
select (unix_timestamp(end_time, 'HH:mm') 
- unix_timestamp(start_time, 'HH:mm'))/60 from  temp.test_table;

结果:30.0