hive秒数如何转化为时分秒

很多报表里面都会有人均使用时长这个概念,来统计用户使用app的深度。
通常我们说的人均使用时长就是拿总使用时长除以总使用人数,除完的结果是秒。
这种秒的结果在页面上就很不直观了。
那我们就会把秒格式化为:12782-->03:33:02

我们来看看这种结果怎么处理,其实这种处理和数字有几个100,几个10,几个1是一样的

1.取小时数据,这个时候我们要取整floor(12782/3600),并且做判断是一位数还是两位数,如果是一位数我们得在前面补一下“0”,并在后面拼接一个“:”,如果是两位数就在后面拼接一个“:”,如果没有够小时就拼接一个“00:”(下面的分和秒也一样)

  case when length(floor(12782/3600))=1 then concat('0',floor(12782/3600),':')
       when length(floor(12782/3600))=2 then concat(floor(12782/3600),':')
       else '00:' end

2.取分钟数据,分钟数据是除完小时(3600)后的余数,再除60取整数floor(12782%3600/60)

  case when length(floor(12782%3600/60))=1 then concat('0',floor(12782%3600/60),':')
       when length(floor(12782%3600/60))=2 then concat(floor(12782%3600/60),':')
       else '00:' end

3.最后是秒数,取完小时再取完分钟后的余数floor(12782%3600%60)

  case when length(floor(12782%3600%60))=1 then concat('0',floor(12782%3600%60))
       when length(floor(12782%3600%60))=2 then floor(12782%3600%60)
       else '00' end
我们来看一下整体的结果:
spark-sql> select
         >   concat(
         >   case when length(floor(12782/3600))=1 then concat('0',floor(12782/3600),':')
         >        when length(floor(12782/3600))=2 then concat(floor(12782/3600),':')
         >        else '00:' end,
         >   case when length(floor(12782%3600/60))=1 then concat('0',floor(12782%3600/60),':')
         >        when length(floor(12782%3600/60))=2 then concat(floor(12782%3600/60),':')
         >        else '00:' end,
         >   case when length(floor(12782%3600%60))=1 then concat('0',floor(12782%3600%60))
         >        when length(floor(12782%3600%60))=2 then floor(12782%3600%60)
         >        else '00' end
         >        );
03:33:02
Time taken: 0.364 seconds, Fetched 1 row(s)
spark-sql> select
         >   concat(
         >   case when length(floor(127820/3600))=1 then concat('0',floor(127820/3600),':')
         >        when length(floor(127820/3600))=2 then concat(floor(127820/3600),':')
         >        else '00:' end,
         >   case when length(floor(127820%3600/60))=1 then concat('0',floor(127820%3600/60),':')
         >        when length(floor(127820%3600/60))=2 then concat(floor(127820%3600/60),':')
         >        else '00:' end,
         >   case when length(floor(127820%3600%60))=1 then concat('0',floor(127820%3600%60))
         >        when length(floor(127820%3600%60))=2 then floor(127820%3600%60)
         >        else '00' end
         >        )
         > ;
35:30:20
Time taken: 0.367 seconds, Fetched 1 row(s)
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值