newkid找到的81978100367936已经是符合条件最大的了
49#说的另外个数字是14位里面最长的了。剩下的就更小了
一个SQL能写出,但是效率太低了,拆成多个SQL了。
本来想构造20位的数字,发现实在构造不出来,太慢太慢了。
又想着用4位符合条件的数字拼出20位来
最终发现单个SQL效率太低,跑不出来结果。拆分成多个语句跑出来结果的- drop table test;
- create table test as
- with tm as (select lpad(level,4,'0') lv from dual connect by level<=9999)
- select lv p from (select substr(lv,1,1) as p1, substr(lv,2,1)as p2, substr(lv,3,1)as p3, substr(lv,4,1) as p4,lv from tm)
- where p1+p2+p3+p4 in (1,4,9,16,25,36)
- and REGEXP_COUNT(lv,0) in (0,1,2)
- and REGEXP_COUNT(lv,1) in (0,1,2)
- and REGEXP_COUNT(lv,2) in (0,1,2)
- and REGEXP_COUNT(lv,3) in (0,1,2)
- and REGEXP_COUNT(lv,4) in (0,1,2)
- and REGEXP_COUNT(lv,5) in (0,1,2)
- and REGEXP_COUNT(lv,6) in (0,1,2)
- and REGEXP_COUNT(lv,7) in (0,1,2)
- and REGEXP_COUNT(lv,8) in (0,1,2)
- and REGEXP_COUNT(lv,9) in (0,1,2) ;
- create unique index idx_test on test(p);
- exec dbms_stats.gather_table_ststs(user,'test',cascade=>true);
- drop table test2
- create table test2 as
- select num p from (select num
- ,substr(num,2,4) as num11,substr(num,3,4) as num12,substr(num,4,4) as num13
- from (select t1.p||t2.p as num
- from test t1,test t2
- where (t1.p<>t2.p))
- where REGEXP_COUNT(num,0) in (0,1,2)
- and REGEXP_COUNT(num,1) in (0,1,2)
- and REGEXP_COUNT(num,2) in (0,1,2)
- and REGEXP_COUNT(num,3) in (0,1,2)
- and REGEXP_COUNT(num,4) in (0,1,2)
- and REGEXP_COUNT(num,5) in (0,1,2)
- and REGEXP_COUNT(num,6) in (0,1,2)
- and REGEXP_COUNT(num,7) in (0,1,2)
- and REGEXP_COUNT(num,8) in (0,1,2)
- and REGEXP_COUNT(num,9) in (0,1,2)
- ) a
- where exists (select 1 from test where p=a.num11)
- and exists (select 1 from test where p=a.num12)
- and exists (select 1 from test where p=a.num13)
- ;
- --substr(num,1,14) 获取构造数字的前多少位,已经发现至少14才有符合条件的了
- select num from (select substr(num,1,14) as num
- ,substr(num,6,4) as num11,substr(num,7,4) as num12,substr(num,8,4) as num13
- from (select t1.p||t2.p as num
- from test2 t1,test2 t2
- where (t1.p<>t2.p))
- ) a
- where exists (select 1 from test where p=a.num11)
- and exists (select 1 from test where p=a.num12)
- and exists (select 1 from test where p=a.num13)
- and REGEXP_COUNT(num,0) in (0,1,2)
- and REGEXP_COUNT(num,1) in (0,1,2)
- and REGEXP_COUNT(num,2) in (0,1,2)
- and REGEXP_COUNT(num,3) in (0,1,2)
- and REGEXP_COUNT(num,4) in (0,1,2)
- and REGEXP_COUNT(num,5) in (0,1,2)
- and REGEXP_COUNT(num,6) in (0,1,2)
- and REGEXP_COUNT(num,7) in (0,1,2)
- and REGEXP_COUNT(num,8) in (0,1,2)
- and REGEXP_COUNT(num,9) in (0,1,2)
- ;
- --63976300187918
- --81978100367936
复制代码 --以下是单个SQL,希望大神给我优化下- with tm as (select lpad(level,4,'0') lv from dual connect by level<=9999)
- ,test as (select lv p from (select substr(lv,1,1) as p1, substr(lv,2,1)as p2, substr(lv,3,1)as p3, substr(lv,4,1) as p4,lv from tm)
- where p1+p2+p3+p4 in (1,4,9,16,25,36)
- and REGEXP_COUNT(lv,0) in (0,1,2)
- and REGEXP_COUNT(lv,1) in (0,1,2)
- and REGEXP_COUNT(lv,2) in (0,1,2)
- and REGEXP_COUNT(lv,3) in (0,1,2)
- and REGEXP_COUNT(lv,4) in (0,1,2)
- and REGEXP_COUNT(lv,5) in (0,1,2)
- and REGEXP_COUNT(lv,6) in (0,1,2)
- and REGEXP_COUNT(lv,7) in (0,1,2)
- and REGEXP_COUNT(lv,8) in (0,1,2)
- and REGEXP_COUNT(lv,9) in (0,1,2))
- ,test2 as (select num p from (select num
- ,substr(num,2,4) as num11,substr(num,3,4) as num12,substr(num,4,4) as num13
- from (select t1.p||t2.p as num
- from test t1,test t2
- where (t1.p<>t2.p))
- where REGEXP_COUNT(num,0) in (0,1,2)
- and REGEXP_COUNT(num,1) in (0,1,2)
- and REGEXP_COUNT(num,2) in (0,1,2)
- and REGEXP_COUNT(num,3) in (0,1,2)
- and REGEXP_COUNT(num,4) in (0,1,2)
- and REGEXP_COUNT(num,5) in (0,1,2)
- and REGEXP_COUNT(num,6) in (0,1,2)
- and REGEXP_COUNT(num,7) in (0,1,2)
- and REGEXP_COUNT(num,8) in (0,1,2)
- and REGEXP_COUNT(num,9) in (0,1,2)
- ) a
- where exists (select 1 from test where p=a.num11)
- and exists (select 1 from test where p=a.num12)
- and exists (select 1 from test where p=a.num13))
- select num from (select substr(num,1,14) as num
- ,substr(num,6,4) as num11,substr(num,7,4) as num12,substr(num,8,4) as num13
- from (select t1.p||t2.p as num
- from test2 t1,test2 t2
- where (t1.p<>t2.p))
- ) a
- where exists (select 1 from test where p=a.num11)
- and exists (select 1 from test where p=a.num12)
- and exists (select 1 from test where p=a.num13)
- and REGEXP_COUNT(num,0) in (0,1,2)
- and REGEXP_COUNT(num,1) in (0,1,2)
- and REGEXP_COUNT(num,2) in (0,1,2)
- and REGEXP_COUNT(num,3) in (0,1,2)
- and REGEXP_COUNT(num,4) in (0,1,2)
- and REGEXP_COUNT(num,5) in (0,1,2)
- and REGEXP_COUNT(num,6) in (0,1,2)
- and REGEXP_COUNT(num,7) in (0,1,2)
- and REGEXP_COUNT(num,8) in (0,1,2)
- and REGEXP_COUNT(num,9) in (0,1,2)
- ;
复制代码 |