sqlserver递归排序
Posted 神色自若
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver递归排序相关的知识,希望对你有一定的参考价值。
此算法不支持无限递归,只支持指定最大层级,实际应用中,一般不会超过5级,sqlserver最大只支持100级。
递归层级LevelOrder序号,每层级最大序号sequences,
子级序号=父级序号+父级序号/最大序号
即LevelOrder=p.LevelOrder+p.LevelOrder/sequences
说明:每级最大序号为10,LevelOrder递归如果为5级,即10000(即有最小5位数,因为每递归一级,除10少一个0),如果为6级即LevelOrder100000(即有6位数),每级最大序号为10
示例代码:
declare @LevelOrder int=10000,@sequences int=10
declare @t table
(
Id int, Name varchar(max), ParentId int
);
insert into @t
select 1,'一级1',0 union all
select 2,'二级1',1 union all
select 3,'三级1',2 union all
select 4,'三级2',2 union all
select 5,'三级4',2 union all
select 6,'四级1',3 union all
select 7,'四级2',3 union all
select 9,'四级3',4 union all
select 10,'四级4',4 union all
select 11,'一级2',0 union all
select 13,'三级5 ',12 union all
select 12,'二级2',11 union all
select 14,'三级标记',12 union all
select 15,'四级11',13 union all
select 16,'四级12',13 union all
select 17,'四级13',13 union all
select 18,'四级14',13 union all
select 18,'四级15',13 union all
select 18,'四级16',13 union all
select 18,'四级17',13 union all
select 18,'四级18',13 union all
select 18,'四级19',13 union all
select 18,'四级20',13 union all
select 18,'四级21',13
;WITH recursiveOrder AS
(
SELECT Id,
Name,
ParentId,
(ROW_NUMBER()over(order by GETDATE()) * @LevelOrder) AS LevelOrder,
@LevelOrder AS sequences
FROM @t
WHERE ParentId =0
UNION ALL
SELECT si.Id,
si.Name,
si.ParentId,
(p.LevelOrder + ROW_NUMBER()over(order by GETDATE()) * p.sequences/ @sequences) as LevelOrder,
(p.sequences / @sequences) as sequences
FROM @t si INNER JOIN recursiveOrder p
ON si.ParentId = p.Id
)
SELECT * FROM recursiveOrder ORDER BY LevelOrder
如果你的最大序号很大,计算LevelOrder位数为递归层级*(序号位数-1)
例如:
1.最大序号为10,递归层级为5,5*(2位数-1)=5位数,LevelOrder为10000
2.最大序号为100,递归层级为5,5*(3位数-1)=10位数,LevelOrder为1000000000
带缩进显示
;WITH recursiveOrder AS
(
SELECT Id,
Name,
ParentId,0 Step,
(ROW_NUMBER()over(order by GETDATE()) * @LevelOrder) AS LevelOrder,
@LevelOrder AS sequences
FROM @t
WHERE ParentId =0
UNION ALL
SELECT si.Id,
si.Name,
si.ParentId,p.Step+1,
(p.LevelOrder + ROW_NUMBER()over(order by GETDATE()) * p.sequences/ @sequences) as LevelOrder,
(p.sequences / @sequences) as sequences
FROM @t si INNER JOIN recursiveOrder p
ON si.ParentId = p.Id
)
SELECT Id,REPLICATE(' ', Step) + name FROM recursiveOrder ORDER BY LevelOrder
如果你有搞不定的问题,请加微信25489181,欢迎提出高质量问题。
以上是关于sqlserver递归排序的主要内容,如果未能解决你的问题,请参考以下文章