hql练习:区间上下限问题

表结构

-- 表1
小明	26
小强	45
小司	57
小武	12
小高	80
小陈	99
小张	45
小李	77
小红	93
小赵	90

-- 表2
0
30
60
80
100

-- 建表
create table if not exists score(
name string, score string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/score';

create table if not exists edge(
 val string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/edge';

要求最终展示的效果如下:

在这里插入图片描述

思路

-- 第一步:先构建分数区间
select concat(val, '-', edge_max) as score_range
from (
         SELECT val, lead(val, 1) OVER (ORDER BY CAST(val AS int)) AS edge_max
         FROM edge
) B1 where edge_max is not null;

如下:
score_range
0-30
30-60
60-80
80-100
-- 第二步:组装想要的数据,获得的是笛卡尔积,后续优化
select A.*,
       split(B.score_range, '-')[0] score_min,
       split(B.score_range, '-')[1] score_max,
       B.score_range
from (select name, score from score) A,
     (
         select concat(val, '-', edge_max) as score_range
         from (
                  SELECT val, lead(val, 1) OVER (ORDER BY CAST(val AS int)) AS edge_max
                  FROM edge
              ) B1
         where edge_max is not null
     ) B;

在这里插入图片描述

-- 第三步:过滤数据,得到最终结果
select name,
       score,
       if(cast(score as int) <= cast((score_min + score_max) as int) / 2 , score_min, score_max) as near_edge,
       score_range
from (
         select A.*,
                split(B.score_range, '-')[0] score_min,
                split(B.score_range, '-')[1] score_max,
                B.score_range
         from (select name, score from score) A,
              (
                  select concat(val, '-', edge_max) as score_range
                  from (
                           SELECT val, lead(val, 1) OVER (ORDER BY CAST(val AS int)) AS edge_max
                           FROM edge
                       ) B1
                  where edge_max is not null
              ) B
     ) T
where cast(T.score as int) > cast(T.score_min as int)
  and cast(T.score as int) < cast(T.score_max as int);

在这里插入图片描述

学习lead()函数的用法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值