0
点赞
收藏
分享

微信扫一扫

hivesql - 模拟循环操作

kolibreath 2022-04-08 阅读 52

表结构

image.png

建表

create table t8
(
    a string
);

insert into t8
values ('1011'),
       ('0101');

1)取出字符串中’1’的位置

输出结果

image.png

思路

  1. 0100字符串首先要切割,获取字符串的数组
  2. 炸裂函数posexplode()可以取出当前索引,然后+1
  3. 再用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;

举报

相关推荐

0 条评论