Hadoop中条件控制语句的使用方法case when(实例)

这篇博客通过实例详细介绍了如何在Hadoop的HQL(Hive SQL)中使用case when语句进行条件控制,以解决年龄和薪资的分类问题。案例中展示了如何根据年龄划分少年、青年、中年和老年,以及根据薪资划分不同等级。通过创建casewhen表并导入数据,然后编写HQL查询语句,实现了数据的分类处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据:
1,zhangsan,18,beijing,20000
2,lisi,28,shanghai,1500
3,wangwu,38,wuhan,4000
4,zhaoliu,35,changsha,10050
5,tianqi,23,shijiazhuang,30000
6,wangba,45,qinhuangdao,28000
7,wushuai,55,haerbin,80000
//创建casewhen表

create table if not exists casewhen(
id int,
name string,
age int,
addr string,
income int
)
row format delimited fields terminated by ','
stored as textfile;

//导入数据

load data local inpath 'home/bigdate/hive/hive/data/casewhen.txt' into table casewhen;

//需求:
---------1.年龄:年龄小于等于20是少年,大于20小于等于30青年,大于30小于等于60都算中年,大于60算老年;

---------2.薪资:薪资小于等于5000是小白,薪资大于5000小于等于15000是董,薪资大于15000小于等于30000是还行,薪资大于30000小于等于60000是还不错,薪资大于60000是考虑;

//casewhen的语法使用(按需求来决定)
case
when age<=20 then ‘少年’
when age>20 and age<=30 then ‘青年’
when age>30 and age<=60 then ‘中年’
when age>60 then ‘老年’
end as status,

case
when income<=5000 then ‘小白’
when income>5000 and income<=15000 then ‘小董’
when income>15000 and income<=30000 then ‘还行’
when income>30000 and income<=60000 then ‘还不错’
when income>60000 then ‘考虑’
end as level

//HQL查询语句

select 
id,
name,
age,
case
when age<=20 then '少年'
when age>20 and age<=30 then '青年'
when age>30 and age<=60 then '中年'
when age>60 then '老年'
end as status,
addr,
income,
case
when income<=5000 then '小白'
when income>5000 and income<=15000 then '小董'
when income>15000 and income<=30000 then '还行'
when income>30000 and income<=60000 then '还不错'
when income>60000 then '考虑'
end as level
from casewhen;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值