Oracle - 如果令牌数少于2

时间:2016-07-21 14:06:46

标签: sql oracle11g

我有像......这样的记录。

    ID | KEY
-------|---------
    1  | 123_456_abc
    1  | 123_xyz
    1  | 456_abc
    2  | 123_abc
    2  | 122_73_zcc
    3  | 123_wer
    4  | 345_23_fhd
    4  | 3453_abc
    5  | ad1fr2h3_abcasd
    5  | ers2g45bb_abc2rtd
    5  | asf23g_abc1_sf45

我想要count(ID) where count(tokanize(numeric(KEY),'_')) < 2

由于计数(ID)为6

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情

{{1}}

这应该过滤掉少于两个下划线的所有元素。

答案 1 :(得分:0)

删除所有超过1个下划线的记录 然后消除不以数字开头的那些 总结一下

select sum(cnt) from (
select key, cnt,  id from (
select key, length(regexp_replace(key,'[^_]*','')) cnt, id from table_name
) where cnt < 2

) where regexp_like(key,'[1-9]+(.)*')

答案 2 :(得分:0)

试试这个:

select Count(1) from 
(with abc(id,key) as (select '1','123_456_abc' from dual
                     Union all
                     select '1','123_xyz' from dual
                     UNion all
                     select '1','456_abc' from dual
                     Union all
                     select '2','123_abc' from dual
                     UNion all
                     select '2','123_73_zcc' from dual
                     Union all
                     select '3','123_wer' from dual
                     UNion all
                     select '1','345_23_fhd' from dual
                     UNion all
                     select '1','345_abc' from dual

)
select key, length(regexp_replace(key,'[^_]*','')) cntr 
from abc )
where cntr = 1