Hive:如何计算时差 [英] Hive: How to calculate time difference
问题描述
我的要求很简单,如何计算hive中两列之间的时间差
My requirement is simple how to calculate the time difference between two column in hive
示例
时间_开始:10:15:00
时间_结束:11:45:00
我需要做 (Time_End-Time_Start) =1:30:00
I need to do (Time_End-Time_Start) =1:30:00
注意这两列都是字符串数据类型,请帮助获得所需的结果..
Note both the columns are in String datatype kindly help to get required result..
推荐答案
语言手册 包含所有可用日期时间函数的描述.秒差可以这样计算:hour(time_end) * 3600 + minute(time_end) * 60 + second(time_end) - hour(time_start) * 3600 - minute(time_start) * 60 - second(time_start)代码>.您可以使用
from_unixtime(..., 'HH:mm:ss')
包装它以获得格式化的时间差异.
Language manual contains description of all available datetime functions. Difference in seconds can be calculated in such way: hour(time_end) * 3600 + minute(time_end) * 60 + second(time_end) - hour(time_start) * 3600 - minute(time_start) * 60 - second(time_start)
. You can wrap it with from_unixtime(..., 'HH:mm:ss')
to get formatted time diff.
编写 UDF 可能更好而不是将所有这些都放入您的查询中.
It might be better to write a UDF instead of putting all this into your query.
这篇关于Hive:如何计算时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!