4

想象一下我有一张像

Name
----
ABCDEFG
ABChello world
ABCDEfoo
ABbar
ABCDEF
ABCDEFGHIJKLMNOP
zzz
qABCD
ABCqqqGH
ABCABC

我想做一个查询并找出每个字符串有多少个字符与所需的字符串“ABCDEFGHIJ”相匹配,总是从头开始。那是...

Name               MatchingLength
----               ----
ABCDEFG            7
ABChello world     3
ABCDEzoo           5
ABbar              2
ABCDEF             6
ABCDEFGHIJKLMNOP   10
zzz                0
qABCD              0
ABCqqqGH           3
ABCABC             3

有没有办法在 Oracle 中干净地做到这一点?我不知所措。

4

6 回答 6

4

不知道“干净”,但这里有两个解决方案。

-- The hardcoded, bad performance. No transformation of your string though.
with patterns as (
        select substr('ABCDEFGHIJ', 1, rownum) txt
        from dual 
        connect by level <= length('ABCDEFGHIJ')
    )
select d.txt, coalesce(max(length(p.txt)), 0)
from dummy d
    left join patterns p
        on instr(d.txt, p.txt) = 1
group by d.txt
order by 2 desc;

-- The cool one with regex. 
-- Though transforming your input string, 
-- this can also be done with ease making something that transorms it for you
-- like in the previous example, more complicated task than the previous,
-- as oracle sucks with string manipulation. You can however write it in java.
select d.txt, coalesce(LENGTH(REGEXP_SUBSTR(d.txt, '^A(B(C(D(E(F(G(H(I(J)?)?)?)?)?)?)?)?)')), 0)
from dummy d;

http://www.sqlfiddle.com/#!4/85ba6/23

更新

with patterns as (
        select substr('ABCDEFGHIJ', 1, rownum) txt
        from dual 
        connect by level <= length('ABCDEFGHIJ')
    )
select d.txt, coalesce(max(length(p.txt)), 0)
from dummy d
    left join patterns p
        on instr(d.txt, p.txt) = 1
where d.txt LIKE substr('ABCDEFGHIJ', 1, 1) || '%'
group by d.txt
order by 2 desc;

更新小提琴:http ://www.sqlfiddle.com/#!4/37400/6

在 oracle 10g 上测试生成的查询计划

SELECT STATEMENT, GOAL = ALL_ROWS       
 SORT ORDER BY                  
  SORT GROUP BY NOSORT          
   NESTED LOOPS OUTER           
    INDEX RANGE SCAN    I <<<< Uses the index.
    VIEW                            
     COUNT                  
      CONNECT BY WITHOUT FILTERING                  
       FAST DUAL                    
于 2013-03-21T04:05:00.577 回答
0

假设您只想获得那些以开头 ABCDEFGHIJ的字符串的匹配计数,并且对于匹配计数这样的字符串qABCD将是0

 SELECT STR,DECODE(SUBSTR(STR,1,1),'A',LENGTH(STR)-
 NVL(LENGTH(REPLACE(TRANSLATE(STR,'ABCDEFGHIJ',' '),' ','')),0),0) MATCHING_LENGTH FROM table
于 2013-03-21T04:59:57.633 回答
0

如果您使用的是 Oracle 11gR2,那么您可以像这样使用递归公用表表达式

with rcte(txt,t, p, c) as
(
  select d.txt , d.txt t, 'ABCDEFGHIJ' p, 0 c  
  from dummy d
  union all
  select txt ,substr(t, 2), substr(p, 2), case when substr(t, 1, 1) = substr(p, 1, 1) then 1 else 0 end 
  from rcte 
  where length(t) > 0  and length(p) > 0  and substr(t, 1, 1) = substr(p, 1, 1)
  )
select txt, sum(c) from rcte
group by txt;

这是一个 sqlfiddle 演示(感谢@Roger)

于 2013-03-21T06:41:24.550 回答
0

假设匹配条件是符号和位置相等(模式ABCqqqGH的值为 5),您可以尝试以下操作:

17:57:03 SYSTEM@sandbox> @sf test

VAL
------------------------------
ABCDEFG
ABChello world
ABCDEzoo
ABbar
ABCDEF
ABCDEFGHIJKLMNOP
zzz
qABCD

8 rows selected.

Elapsed: 00:00:00.01
17:57:05 SYSTEM@sandbox> @get match
  1  select t.val, count(l)
  2    from test t
  3    left join (select level l from dual connect by level <= length('ABCDEFGHIJ')) i
  4      on substr(t.val, i.l, 1) = substr('ABCDEFGHIJ', i.l, 1)
  5   group by t.val
  6*  order by 2 desc
17:57:07 SYSTEM@sandbox> /

VAL                              COUNT(L)
------------------------------ ----------
ABCDEFGHIJKLMNOP                       10
ABCDEFG                                 7
ABCDEF                                  6
ABCDEzoo                                5
ABChello world                          3
ABbar                                   2
zzz                                     0
qABCD                                   0

8 rows selected.

Elapsed: 00:00:00.02
于 2013-03-21T10:00:01.623 回答
0
declare
       v_1 number := 0;
v_pattern VARCHAR(26) := '&n';
v_f number;
v_spaces VARCHAR(30) := ' ';
v_l number;
v_c varchar(20) := ' ';
v_n varchar(20) := ' ';
    BEGIN
 v_f := Ascii(SubStr(v_pattern,1,1));
v_l := Ascii(SubStr(v_pattern,Length(v_pattern)));
v_spaces := LPad(' ',Length(v_pattern),' ');
for i in (select str,TRANSLATE(REPLACE(str,' ',''),v_pattern,v_spaces) c1,length(REPLACE(str,' ',''))-nvl(length(replace(TRANSLATE(REPLACE(str,' ',''),v_pattern,v_spaces),' ','')),0) c2 from table 
where ascii(substr(str,1,1)) IN (SELECT DISTINCT Ascii(SubStr(v_pattern,LEVEL,1)) FROM dual CONNECT BY LEVEL<=Length(v_pattern))) loop
       for j in 1..i.c2 loop
               v_c :=instr(i.c1,' ',1,j);
               v_n :=instr(i.c1,' ',1,j+1);
               if v_c+1=v_n then
                          v_1 := v_1+1;
               end if;
       end loop;
                  if(v_1+1 = i.c2) then
               dbms_output.put_line('String : '||i.str||' and Matching count : '||i.c2);
       else
               dbms_output.put_line('String : '||i.str||' and Matching count : '||((v_1)-1));
       end if;
   v_1 := 0;
end loop;
FOR k IN (SELECT str FROM table WHERE NOT(Ascii(substr(str,1,1)) IN (SELECT DISTINCT Ascii(SubStr(v_pattern,LEVEL,1)) FROM dual CONNECT BY LEVEL<=Length(v_pattern)))) LOOP
      dbms_output.put_line('String : '||k.str||' and Matching count : '||v_1);
 END LOOP;
 end;
于 2013-03-21T10:28:40.377 回答
-1

我希望以下内容有所帮助:

CREATE TABLE TESTME ( TNAME VARCHAR2(30));

INSERT INTO TESTME VALUES('ABCDEFG');
INSERT INTO TESTME VALUES('ABChello world');
INSERT INTO TESTME VALUES('ABCDEzoo');
INSERT INTO TESTME VALUES('ABbar');
INSERT INTO TESTME VALUES('ABCDEF');
INSERT INTO TESTME VALUES('ABCDEFGHIJKLMNOP');
INSERT INTO TESTME VALUES('zzz');
INSERT INTO TESTME VALUES('qABCD');  

CREATE OR REPLACE FUNCTION GET_MLENGTH( P_INPUT VARCHAR2)
 RETURN NUMBER 
IS
 -- COMBARING STRING
 C VARCHAR2(10) := ('ABCDEFGHIJ');
 N NUMBER := 0; 
BEGIN 
  FOR I IN 1..LENGTH(P_INPUT) LOOP
   IF SUBSTR(P_INPUT,I,1) = SUBSTR(C,I,1) THEN
    N := N + 1;
   ELSE
    RETURN N; 
   END IF;  
  END LOOP;
RETURN N;   
END;
/


SELECT TNAME , GET_MLENGTH(TNAME) FROM TESTME ;

TNAME                          GET_MLENGTH(TNAME)
------------------------------ ------------------
ABCDEFG                                         7
ABChello world                                  3
ABCDEzoo                                        5
ABbar                                           2
ABCDEF                                          6
ABCDEFGHIJKLMNOP                               10
zzz                                             0
qABCD                                           0
于 2013-03-21T05:53:34.277 回答