Hive: How to calculate time difference

匆匆过客 提交于 2019-12-11 11:48:29

问题


My requirement is simple how to calculate the time difference between two column in hive

Example

Time_Start: 10:15:00

Time_End: 11:45: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..


回答1:


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.

It might be better to write a UDF instead of putting all this into your query.




回答2:


You need to convert the HH:MM:SS times to seconds, get the difference between them and re-arrange it as another HH:MM:SS time.



来源:https://stackoverflow.com/questions/34757504/hive-how-to-calculate-time-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!