わかりました、この解決策は醜く、気弱な人向けではありません。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 テーブルに行がない行は数値です。