如何使用配置单元计算字符串中的唯一整数?

rt4zxlrg  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(320)

尝试计算字符串中的唯一字节数?
数据(例如,电话号码只有数字字节):

1234567890
1111111112

结果:

10
2

我试过下面的方法,但没有成功,因为sum()不会接受带有if的自定义项。

select phone
 , sum(
        cast(if(length(regexp_replace(phone,'0',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'1',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'2',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'3',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'4',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'5',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'6',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'7',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'8',''))<10,'1','0') as int) +
        cast(if(length(regexp_replace(phone,'9',''))<10,'1','0') as int)         
       )  as unique_bytes
 from table;

我也不赞成用正则表达式作为解决方案。

hfyxw5xn

hfyxw5xn1#

使用 + . . . 但就像这样:

select phone,
       ((case when phone like '%0%' then 1 else 0 end) +
        (case when phone like '%1%' then 1 else 0 end) +
        (case when phone like '%2%' then 1 else 0 end) +
        (case when phone like '%3%' then 1 else 0 end) +
        (case when phone like '%4%' then 1 else 0 end) +
        (case when phone like '%5%' then 1 else 0 end) +
        (case when phone like '%6%' then 1 else 0 end) +
        (case when phone like '%7%' then 1 else 0 end) +
        (case when phone like '%8%' then 1 else 0 end) +
        (case when phone like '%9%' then 1 else 0 end) +
       ) as ints
 from table;

您的代码有几个问题: sum() 是聚合函数,不需要。
这个 if() 正在返回字符串,但您正在将值添加到一起。
我不知道你为什么用 regexp_replace() 而不仅仅是 replace() .

rqqzpn5f

rqqzpn5f2#

with tab1 as (
select stack(3,
'1','1234567890',
'2','1111111112',
'3','2222222223') as (col0, col1))
select tab1.col0, count(distinct tf.col) from tab1 lateral view explode(split(tab1.col1,'')) tf as col
where tf.col regexp '\\d'
group by tab1.col0

相关问题