有一段乐谱字串如下:
1265535165656312653126312312
用sql怎样能快事找到预先指定的n位的出现次数最多的字串,如n=3时,
结果为312。
因实际数据量很大,故要考虑效率问题,使用穷举不可行,求教!

解决方案 »

  1.   

    麻烦啊,别用sql了吧,取出来用python java 什么的,感觉可行性大一些,
    等大神来吧
      

  2.   

    这应该也算是穷举法了吧。其他的就想不到了。
    with t1 as
    (select '1265535165656312653126312312' c1 from dual)
    , t2 as
    (select rownum rn from dual connect by rownum<=(select length(c1)-2 from t1))
    select * from (
      select substr(c1, rn, 3) s, count(*) c 
      from t1, t2 
      group by substr(c1, rn, 3) 
      order by count(*) desc
    ) aa 
    where rownum=1 
      

  3.   

    with t1 as
     (select '1265535165656312653126312312' c1 from dual)
    , t2 as(select  substr(c1, level, 3) s, regexp_count(c1, substr(c1, level, 3)) c from t1
    connect by level <= length(c1) - 2 order by c desc)
    select * from t2 where rownum=1;
      

  4.   

    Quote: 引用 7 楼 menper 的回复:

    但拆分后怎么处理呢?另外有无数个这种字串,全部变成列吗,不可行吧?拆分后是行存放的,字符串最大长度才8000了,也就是最多8000行,拆分后再操作,没有难度的。