這裏有一個遞歸,訂貨例如在T-SQL。
declare @table table(
[level] int,
id int,
[name] nvarchar(32),
[parent] nvarchar(32)
);
insert into @table([level], id, [name], parent) values(1, 10, '**Rich**', null);
insert into @table([level], id, [name], parent) values(2, 11, 'Sony', '**Rich**');
insert into @table([level], id, [name], parent) values(1, 13, 'Mary', null);
insert into @table([level], id, [name], parent) values(1, 15, '**John**', null);
insert into @table([level], id, [name], parent) values(2, 12, 'Lily', '**John**');
with rec ([level], id, [name], parent, seq) as (
select
t.[level],
t.id,
t.[name],
t.[parent],
cast(t.[level] as varbinary(max)) as seq
from
@table as t
where
t.parent is null
union all select
t.[level],
t.id,
t.[name],
t.[parent],
r.seq + cast(t.[level] as varbinary(max)) as seq
from
rec as r
inner join @table as t
on t.parent = r.name
)
select
*
from
rec
order by
seq asc
;
要點這裏(1)經由with
遞歸和(2)串聯電平數目的二進制表示成一個單一的序列指示符。
您的表格有4個字段,有些記錄只有3個值。如果你突出顯示你的表並點擊'{}',它將使它成爲一個代碼片段並被均勻分佈(可讀)。另外,你有超過2個關卡嗎?你可以給出輸入數據和所需輸出格式/順序的例子。 – MatBailie 2011-12-19 12:16:44
看起來有人沒有理解他們的功課... – 2011-12-19 12:31:09