2016-09-01 48 views
0

我想計算事件之間的差異。這兩個事件都以毫秒爲單位存儲在UNIX_TIMESTAMP中。HIVE - 時差格式

下面是一個例子:1464740049000

我如何轉換的時間差和時間格式化。

我嘗試了幾個像datediff和/或timestamp(event1) - timestamp(event2)的東西。

我要的是

選擇時間戳(e1.time),時間戳(e2.time),e1.time-e2.time從TESTDATA Time_Diff;

Time_Diff應該以小時,分鐘,秒爲單位格式化... 我該如何得到它? 在此先感謝 彼得

回答

0

如果您有興趣小時而已,取而代之的是unixtimestamp轉換爲時間戳,只需使用SQL數學函數

select (time2 - time1)/(1000 * 60 * 60) as hours from mytable; 
0

如果差小於24小時,你可以用這樣的:

[localhost:21000] > select from_unixtime(1392394861 - 1392394860, 'HH:mm:ss'); 
+----------------------------------------------------+ 
| from_unixtime(1392394861 - 1392394860, 'hh:mm:ss') | 
+----------------------------------------------------+ 
| 00:00:01           | 
+----------------------------------------------------+ 

如果差異可以超過24小時,下面的比較難看錶達將這樣的伎倆:

[localhost:21000] > select concat(cast(floor((1392394861 - 1392300000)/60/60) as string), from_unixtime(1392394861 - 1392300000, ':mm:ss')); 
+----------------------------------------------------------------------------------------------------------------------+ 
| concat(cast(floor((1392394861 - 1392300000)/60/60) as string), from_unixtime(1392394861 - 1392300000, ':mm:ss')) | 
+----------------------------------------------------------------------------------------------------------------------+ 
| 26:21:01                            | 
+----------------------------------------------------------------------------------------------------------------------+ 

或者,如果你喜歡用明確的天格式:

[localhost:21000] > select concat(cast(floor((1392394861 - 1392300000)/60/60/24) as string), " days and ", from_unixtime(1392394861 - 1392300000, 'HH:mm:ss')); 
+-------------------------------------------------------------------------------------------------------------------------------------------+ 
| concat(cast(floor((1392394861 - 1392300000)/60/60/24) as string), ' days and ', from_unixtime(1392394861 - 1392300000, 'hh:mm:ss')) | 
+-------------------------------------------------------------------------------------------------------------------------------------------+ 
| 1 days and 02:21:01                              | 
+-------------------------------------------------------------------------------------------------------------------------------------------+