1

我有一个包含students_id 及其年龄的数据集。我希望标记应排列在桶大小为 10 的范围或 bin 中。

stud_id    ages
101        11
102        13
103        21
104        25

同样,我有更多记录的日期。这必须以 10 的 bin 大小排列。

预期输出为:

stud_id     ages_bin
101         11-20
102         11-20
103         21-30
104         21-30

我在 hive 中尝试了简单的 case 语句。

select stud_id,
case when ages between 0 and 10 then '0-10'
when ages between 11 and 20 then '11-20'
when ages between 21 and 30 then '21-30'
when ages between 31 and 40 then '31-40'
when ages between 41 and 50 then '41-50'
when ages between 51 and 60 then '51-60'
when ages between 61 and 70 then '61-70'
when ages between 71 and 80 then '71-80'
when ages between 81 and 90 then '81-90'
when ages between 91 and 100 then '91-100'
when ages between 101 and 110 then '101-110'
when ages between 111 and 120 then '111-120'
when ages between 121 and 130 then '121-130'
when ages between 131 and 140 then '131-140'
when ages between 141 and 150 then '141-150'
else NULL end as ages_bin
from students

有什么简单的方法可以让桶大小为 10 的分箱数据?

有人可以帮我写一个简单的代码吗?

4

2 回答 2

1

有一种简单的方法可以安排直方图的 bin 范围。这是代码:

select stud_id,floor((ages)/10)*10 as strt_range,
floor((ages)/10)*10+9 as end_range from students

这会产生以下输出:

stud_id     ages_bin
101         10-19
102         10-19
103         20-29
104         20-29
于 2018-06-01T03:40:30.383 回答
0

尝试这个。这应该能够让你获得 bin 格式的 bin:

select stud_id, concat(cast(floor((ages)/10)*10 as string),'-',
cast(floor((ages)/10)*10+9 as string)) from students 

为了能够获得适当的输出,最好将它分组并适当地排序

于 2020-03-29T10:53:24.627 回答