hive中的日期差异和差异应该是hh:mm:ss

时间:2017-08-23 16:25:44

标签: date hadoop hive hiveql

我试图找到连续行中两个日期之间的差异。我正在使用hive中的窗口函数,即lag

但差异即输出应采用hh:mm:ss格式。

例如:

  • 日期1为2017-08-15 02:00:32
  • 日期2为2017-08-15 02:00:20

输出应为:

  

0时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;

但是这会将输出返回为12(在上例中)。

更新

我已使用bigint数据类型将列存储在表中。时间是以纪元格式。我们在查询中使用from_unixtime将其转换为可读日期。时间戳中的示例值

1502802618 1502786788

3 个答案:

答案 0 :(得分:1)

只要时差小于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

答案 1 :(得分:1)

<filter>
    <filter-name>SSOFilter</filter-name>
    <filter-class>com.my.domain.and.company.WSO2SAMLSSOSessionBeanFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SSOFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

答案 2 :(得分:0)

只要时差小于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