反转顺序
反转逗号分隔的数据:改变顺序,不改变内容
create table reverse_str(a string comment '字符串') comment '反转字符串';
insert into reverse_str
values ('AB,CA,BAD'),('BD,EA');
解决1 posexplode
with tmp as(-- 1.posexplode 位置 元素
select a,pos,val
from reverse_str
lateral view posexplode(split(a,',')) pex as pos,val
sort by pos desc -- 2.位置倒序
)
select -- 输出格式
a,
concat_ws(',',collect_list(val)) a_reverse
from tmp
group by a;
解决2 reverse
select
a,reverse(a) a1,
concat_ws(',',collect_list(reverse(val))) a_reverse -- 2.reverse(val) 倒序元素
from reverse_str
lateral view explode(split(reverse(a),',')) ex as val -- 1.reverse(a) 倒序所有字符
group by a,reverse(a);