|
sqlite 不支持在递归时用分析函数,没法实现rownum=1, limit 1的结果不对
- with t6 as(select 1 n union all select n+1 from t6 where n<6)
- ,t as(select 1 lv, n n1 from t6 union all select lv+1, n1*10+n from t,t6
- where lv<6 and instr(n1,n)=0)
- ,tall as(select row_number()over(order by n1)rn,n1 str from t where lv=6)
- , s as (
- select str,
- substr(str,1,3) a,
- substr(str,2,3) b,
- substr(str,3,3) c,
- substr(str,4,3) d
- from tall ),
- r (lvl,rn,str_list,abcd_list) as (select 1,1,
- str,
- a||','||b||','||c||','||d
- from s
- union all
- select lvl+1,row_number()over(order by str)rn,
- r.str_list||','||s.str,
- r.abcd_list||','|| s.a||','||s.b||','||s.c||','||s.d
- from r,s
- where instr(r.str_list,s.str)=0
- and instr(r.abcd_list,s.a)=0
- and instr(r.abcd_list,s.b)=0
- and instr(r.abcd_list,s.c)=0
- and instr(r.abcd_list,s.d)=0
- and rn=1
- )
- --select * from r;
- select max(lvl) from r
- ;
复制代码 |
|