好的,这个解决方案很丑陋,不适合胆小的人。我没有在 SQL CE 上测试过,但它只使用基本的 t-sql,所以应该没问题。您将必须创建一个计数表(只需运行他的第一个代码块,如果您不想阅读它。它使用 tempdb,因此您需要更改它),以及一个保存每个字母的表字母表(由于缺乏模式匹配功能)。创建计数表后(您不必一直到 11000,如示例所示),运行这些,您将看到所需的排序行为
创建字母表(临时用于演示目的):
select *
into #alphatable
from
(
select 'A' as alpha union all
select 'B' union all
select 'C' union all
select 'D'
--etc. etc.
) x
创建一个树表(临时用于演示目的):
select *
into #tree
from
(
select 'aagew' as TreeNumber union all
select '3' union all
select 'bsfreww' union all
select '1' union all
select 'xcaswf'
) x
解决方案:
select TreeNumber
from
(
select t.*, tr.*, substring(TreeNumber, case when N > len(TreeNumber) then len(TreeNumber) else N end, 1) as singleChar
from tally t
cross join #tree tr
where t.N < (select max(len(TreeNumber)) from #tree)
) z
left join
#alphatable a
on z.singlechar = a.alpha
group by TreeNumber
order by case when max(alpha) is not null then 0 else TreeNumber end
这基本上是 Moden 描述为“逐步遍历字符”的技术,然后将每个字符连接到 alpha 表中。alpha 表中没有行的行是数字。