hive 中的日期差异,差异应在 hh:mm:ss

Date diff in hive and the difference should be in hh:mm:ss

我试图找出连续行中两个日期之间的差异。我在配置单元中使用窗口函数,即 lag.

但不同之处在于,输出格式应为 hh:mm:ss

例如:

输出应该是:

00:00:12

我试过的查询:

select from_unixtime(column_name),
(lag(unix_timestamp(from_unixtime(column_name)),1,0)
over(partition by column_name)-
unix_timestamp(from_unixtime(column_name))) as Duration from table_name;

但是这个returns输出为12(在上面的例子中)。

更新

我用 bigint 数据类型将列存储在 table 中。时间采用纪元格式。我们在查询中使用 from_unixtime 将其转换为可读日期。时间戳中的样本值

1502802618 1502786788

只要时差小于24小时,答案就会相关

hive> with t as (select timestamp '2017-08-15 02:00:32' as ts1,timestamp '2017-08-15 02:00:20' as ts2)
    > select  ts1 - ts2   as diff
    > from    t
    > ;
OK
diff
0 00:00:12.000000000

给定时间戳

hive> with t as (select timestamp '2017-08-15 02:00:32' as ts1,timestamp '2017-08-15 02:00:20' as ts2)
    > select  split(ts1 - ts2,'[ .]')[1]  as diff
    > from    t
    > ;
OK
diff
00:00:12

给定的字符串

hive> with t as (select '2017-08-15 02:00:32' as ts1,'2017-08-15 02:00:20' as ts2)
    > select  split(cast(ts1 as timestamp) - cast(ts2 as timestamp),'[ .]')[1]  as diff
    > from    t
    > ;
OK
diff
00:00:12

只要时差小于24小时,答案就是相关的

hive> with t as (select 1502802618 as ts1,1502786788 as ts2)
    > select  from_unixtime(to_unix_timestamp('0001-01-01 00:00:00')+(ts1 - ts2))  as diff
    > from    t
    > ;
OK
diff
0001-01-01 04:23:50

hive> with t as (select 1502802618 as ts1,1502786788 as ts2)
    > select  substr(from_unixtime(to_unix_timestamp('0001-01-01 00:00:00')+(ts1 - ts2)),12)   as diff
    > from    t
    > ;
OK
diff
04:23:50
hive> with t as (select 1502802618 as ts1,1502786788 as ts2)
    > select  printf('%02d:%02d:%02d',(ts1 - ts2) div 3600,((ts1 - ts2) % 3600) div 60,((ts1 - ts2) % 3600) % 60) as diff
    > from    t
    > ;
OK
diff
04:23:50