Hive浮点数的几种不同取值

60 篇文章 2 订阅
1、round:四舍五入
round(DOUBLE d) : 返回DOUBLE型的d的BIGINT类型的近似值 
round(DOUBLE d,INT) : 返回DOUBLE型的d的保留n位小数的DOUBLE类型的近似值 

2、ceil:向上取整
ceil(DOUBLE d): d是DOUBLE类型的,返回>=d的最小的BIGINT值 
3、floor:向下取整
floor(DOUBLE d): d是DOUBLE类型的,返回<=d的最大的BIGINT值 
4、取随机数
rand() rand(INT seed): 每行返回一个DOUBLE型的随机数,整数seed是随机因子 

需求:
每天直播时长超过半小时算半小时,不到半小时舍去算直播时长。
excel中有直接的公式可以取,但Hive中好似不行。
=FLOOR(A1,0.5)  excel中

select floor(3.4)  
select substr(round(123.58,1),-1,1);
select round(123.58,1)
最终,hive可以这样实现需求:
select floor(123.58)+(case when int(substr(round(123.58,1),-1,1))>=5 then 0.5 else 0 end);
实际的计算脚本:
select room_id,count(case when live_hours>=1 then pt_day else null end) eff_live_days,sum(live_hours) live_hours,sum(floor(live_hours)+(case when int(substr(round(live_hours,1),-1,1))>=5 then 0.5 else 0 end)) live_hours_half
from (select room_id,pt_day,sum(unix_timestamp(updated_time)-unix_timestamp(switch_time))/60/60 live_hours
from honeycomb_all_live_history_status 
where pt_month='2018-04' and category_id=111
group by room_id,pt_day) x
group by room_id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值