Hive浮点数的几种不同取值

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;

猜你喜欢

转载自blog.csdn.net/babyfish13/article/details/80179434