|
CTE中不能使用GROUPBY ,DISTINCT,不能UNION ALL 后的SELECT外再包一层,所以ROW_NUMBER的
方法也被我放弃了,今天看到你的去重写法,很好!
引用一下你的去除重复的方法:
SQL> set timing on;
SQL>
SQL>
SQL> with point(n,x,y) as (select level n,ceil(level/3),decode(mod(level,3),0,3,mod(level,3)) from dual connect by level<=9),
2 line(n,p1,p2) as (select rownum,p1.n,p2.n from point p1,point p2 where abs(p1.x - p2.x) + abs(p1.y-p2.y) =1 and p1.n < p2.n),
3 shape(lvl,nlist,val,plist,rn) as (select 1,cast(n as varchar2(100)),power(2,n),p1||','||p2,1 from line
4 union all
5 select s.lvl + 1,
6 s.nlist||','||a.n,
7 s.val + power(2,a.n),
8 s.plist||','||a.p1||','||a.p2,
9 row_number() over(partition by s.lvl+1,s.val + power(2,a.n) order by s.val + power(2,a.n)) rn
10 from shape s,line a
11 where bitand(s.val,power(2,a.n))=0
12 and (instr(s.plist,a.p1) > 0 or instr(s.plist,a.p2) > 0)
13 and lvl < 12
14 and rn = 1
15 )
16 select sum(cnt)
17 from (select lvl, count(distinct val) cnt from shape group by lvl)
18 /
SUM(CNT)
----------
1759
Executed in 0.094 seconds
|
|