表结构
建表
create table t8
(
a string
);
insert into t8
values ('1011'),
('0101');
1)取出字符串中’1’的位置
输出结果
思路
0100
字符串首先要切割,获取字符串的数组- 炸裂函数
posexplode()
可以取出当前索引,然后+1 - 再用
concat_ws()
拼接到一起
SQL
select a,
concat_ws(',', collect_list(cast(index as string)))
from (select
index + 1 as index,
a,
chr
from (select a,
concat_ws(',', substr(a, 1, 1), substr(a, 2, 1), substr(a, 3, 1), substr(a, 4, 1)) as arr
from t8) tmp1 lateral view posexplode(split(arr, ',')) t as index, chr
where chr = '1') tmp2
group by a;