问题
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