标签:score val -- max 练习 下限 range edge 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()函数的用法
标签:score,val,--,max,练习,下限,range,edge,hql 来源: https://blog.csdn.net/weixin_44178366/article/details/122514946
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。