2016-10-10 8 views
1

문자열의 고유 바이트 수를 계산하려고합니까?하이브를 사용하여 문자열에서 고유 한 정수를 계산하는 방법은 무엇입니까?

데이터 (숫자 만 바이트 예를 들어, 전화 번호) :

1234567890 
1111111112 

결과 : 나는 아래 시도하고 합 때문에 작동하지 않았다

10 
2 

()는 인 경우 UDF를 허용하지 않으므로 '이라고 생각합니다.

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; 

해결책으로 정규 표현식에 사용되지 않습니다.

답변

2

+을 사용하십시오. . . 그러나이 같은 :

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()은 문자열을 반환하지만 값을 함께 추가합니다.
  • replace() 대신 regexp_replace()을 사용하는 이유가 확실하지 않습니다.
+0

고든, 빠른 응답에 감사드립니다. 방정식의 합계가 필요 없습니다. 나는 입국을 택한 직후에 거의 깨달았다. 그러나 솔루션은 훨씬 더 나은 경로를 따라 가고 있으며 더 효율적입니다! 감사. – invoketheshell

+0

게시 할 때 총액 문제가 발생했습니다. 바보 나. 'if'부분은 문자열을 반환하므로 정수로 캐스팅됩니다. 지금까지 regexp_replace를 대체 할 apposed로 사용하는 경우, 저는 많은 정규 표현식을 사용하고 습관을 기본값으로 사용하고있었습니다. 솔루션에 다시 한번 감사드립니다. 그것은 내 것보다 더 효율적이다. 나는 그것을 보았 더라면 좋겠다. 건배. – invoketheshell

관련 문제