1. 数据分箱占比统计
select a.bin
,a.bin_cnt
,concat(round(cast(a.bin_cnt*1.0/b.total_num as decimal(19,4))*100,2),'%') as bin_rate
from (
select case
when a1.exchange_rate<0.2 then '[0.0-0.2)'
when a1.exchange_rate<0.5 then '[0.2-0.5)'
when a1.exchange_rate<0.8 then '[0.5-0.8)'
else '[0.8-1]'
end as bin --换血率分箱
,count(1) as bin_cnt --分箱数量
,'11' as type --用于匹配总数的一个临时标志
from (
select uid
,exchange_rate
from XX
) a1
group by case
when a1.exchange_rate<0.2 then '[0.0-0.2)'
when a1.exchange_rate<0.5 then '[0.2-0.5)'
when a1.exchange_rate<0.8 then '[0.5-0.8)'
else '[0.8-1.0]'
end
) a
left join ( --通过与原表left join 获得总样本数
select count(1) as total_num
,'11' as type
from XX
) b
on a.type=b.type
;
2. Hive实现样本随机采样
order by rand() 函数可以对样本进行随机排序,进而实现hive表数据随机取样。
2.1 rand()
产生一个介于0和1之间的随机数
select rand();
-- 0.5523740163966064
-- 指定随机种子,以后使用这个种子每次产生的随机数都是一样的。
select rand(1234);
-- 0.6465821602909256
2.2 order by rand()
对结果进行随机排序
-- 数据随机排序后取前100条
select uid from tablename order

最低0.47元/天 解锁文章
2万+





