【问题标题】:How to Find the average of hh:mm:ss in hive如何在 hive 中找到 hh:mm:ss 的平均值
【发布时间】:2019-07-27 23:36:35
【问题描述】:

考虑我有一个带有列 script_name、start_time、end_time、duration 的 hive 表。开始时间、结束时间和持续时间的格式为 hh:mm:ss。我的要求是找到这些列最近 7 天的平均时间并放入文件中。

【问题讨论】:

    标签: sql unix hadoop hive hiveql


    【解决方案1】:

    转成unix_timestamp,求和,除以3,转成bigint再转回HH:mm:ss:

    with data as --Data example. Use your table instead
    (select '12:10:30' start_time,'01:10:00' end_time, '02:10:00' duration)
    
    select from_unixtime(cast((unix_timestamp(start_time,'HH:mm:ss')+ unix_timestamp(end_time,'HH:mm:ss')+unix_timestamp(duration,'HH:mm:ss'))/3 as bigint),'HH:mm:ss') from data;
    

    结果:

    05:10:10
    

    对于单列:

    转换为unix时间戳,以秒为单位计算平均值,转换为bigint(平均值为double,会有部分秒精度损失),最后转换回字符串时间格式:

    with data as --Data example. Use your table instead
    (select stack(2,'12:10:30','01:10:00') as timeStr)
    
    select from_unixtime(cast(avg(unix_timestamp(timeStr,'HH:mm:ss'))as bigint),'HH:mm:ss') from data;
    

    结果:

    06:40:15
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 2020-09-01
      • 2011-03-27
      • 2022-06-30
      • 2022-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多