1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| select rand(); select split(rand()*100,"\\.")[0]; select substring(rand()*100,2,2); select regexp_replace("a.jsp","jsp","html"); select cast(1 as double); select case when 1=1 then "man" when 1=2 then "woman" else "yao" end; select concat("1","2"); select concat_ws("|","1","2"); # 连接符 select length("asb");
# 时间戳转字符串 from_unixtime(cast(f.finish_time as int),'yyyy-MM-dd HH:mm:ss') # 字符串转时间戳 unix_timestamp('2018-02-06 00:00:00','yyyy-MM-dd HH:mm:ss')
row_number():没有相同名次,名次不空位 rank():有并列名次,并列名次后将空位 dense_rank():有并列名次,无空位
# 查询每个班级的前三 select tmp.c,tmp.s from ( select r.class c,r.score s,row_number() over (distribute by r.class sort by r.score desc) rr from classinfo r ) tmp where rr<4;
select class,score, rank() over(distribute by class sort by score desc) rank, dense_rank() over(distribute by class sort by score desc) dense_rank, row_number() over(distribute by class sort by score desc) row_number from classinfo;
# 一行变多行,将split之后转化的array替换成一个虚表 # 关键词lateral view explode select id, test.context from demo lateral view explode(split(context, ',')) test as context;
# 多行变一行,会去重,将分组之后的context放入集合中,并以','进行分割 # 如果不使用concat_ws,结果就是集合 select id,concat_ws(',',collect_set(context)) from demo group by id;
# 多行多列变一行,先进行字符串拼接,在存入集合中 select a.id,concat_ws('|',collect_set(a.info)) from ( select id,concat_ws(',',context,dt) as info from demo ) a group by a.id;
|