数据:
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;