11,849
社区成员
发帖
与我相关
我的任务
分享
138 42 5796 138425796
157 28 4396 157284396
159 48 7632 159487632
186 39 7254 186397254
198 27 5346 198275346
297 18 5346 297185346
483 12 5796 483125796
1738 4 6952 173846952
1963 4 7852 196347852
--看着挺笨重的,我本地运行250-300毫秒之间。
;with m as (
select number as id from master..spt_values where type = 'p' and number between 1 and 9),
m1 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2 ,ltrim(t3.id) as c3 ,ltrim(t4.id) as c4
from m t1 cross join m t2 cross join m t3 cross join m t4
where t1.id <> t2.id and t1.id <> t3.id and t1.id <> t4.id
and t2.id <> t3.id and t2.id <> t4.id and t3.id <> t4.id
),
m2 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2 ,ltrim(t3.id) as c3
from m t1 cross join m t2 cross join m t3
where t1.id <> t2.id and t1.id <> t3.id and t2.id <> t3.id
),
m3 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2
from m t1 cross join m t2 where t1.id <> t2.id
),
m4 as ( select (a.c1 + a.c2 + a.c3) as c1 ,( b.c1 + b.c2 ) as c2 ,
ltrim(( a.c1 + a.c2 + a.c3 ) + ( b.c1 + b.c2 )+ ltrim(( a.c1 + a.c2 + a.c3 ) * 1 * ( b.c1+ b.c2 ))) as c3
from m2 a ,m3 b
where ( a.c1 + a.c2 + a.c3 ) * 1 * ( b.c1 + b.c2 ) < 9876 union all
select ( a.id ) ,( b.c1 + b.c2 + b.c3 + b.c4 ) ,
ltrim(ltrim(a.id) + ( b.c1 + b.c2 + b.c3 + b.c4 )+ ltrim(a.id * 1 * ( b.c1 + b.c2 + b.c3 + b.c4 )))
from m a ,m1 b
where a.id * 1 * ( b.c1 + b.c2 + b.c3 + b.c4 ) < 9876
)
select c1 ,c2 ,c3 from m4
where len(c3) = 9
and charindex('1', c3) > 0
and charindex('2', c3) > 0
and charindex('3', c3) > 0
and charindex('4', c3) > 0
and charindex('5', c3) > 0
and charindex('6', c3) > 0
and charindex('7', c3) > 0
and charindex('8', c3) > 0
and charindex('9', c3) > 0
--try
),t1 as
(
select id from #tb a
where id between 1 and 99
and not exists (select 1 from t0 where len(ltrim(a.id))-len(replace(ltrim(a.id),ltrim(id),'')) >= 2)
)