22,209
社区成员
发帖
与我相关
我的任务
分享
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(25),[Date] Date,[ParentID] int,[Layer] int)
Insert #T
select 1,N'A','2016-4-1',null,0 union all
select 2,N'A-1','2016-4-2',1,1 union all
select 3,N'A-1-1','2016-4-5',2,2 union all
select 4,N'A-1-2','2016-4-4',2,2 union all
select 5,N'A-2','2016-4-6',1,1 union all
select 6,N'B','2016-3-1',null,0 union all
select 7,N'B-1','2016-5-1',6,1 union all
select 8,N'B-2','2016-4-1',6,1 union all
select 9,N'A-111111','2016-4-1',1,1 union all
select 10,N'A-111111-1','2016-5-1',9,2 union all
select 11,N'A-111111-2','2016-4-1',9,2
Go
;with t as
(select *,
tname=SUBSTRING(name,1,len(name)-charindex('-',reverse(name))+1),
maxL=MAX(layer)over(partition by left(name,1))
from #t)
select * from t
order by (case when Layer=maxL then tname else Name end),date
;WITH t AS (
SELECT *,[name]+REPLICATE('-0',MAX([Layer]) OVER (PARTITION BY LEFT([name],1))-isnull([Layer],0) ) AS newname , MAX([Layer]) OVER (PARTITION BY LEFT([name],1)) AS MaxLayer
FROM #t
)
SELECT *,ISNULL(a.pNewName,'') +convert(VARCHAR, t.[date],101) FROM t
OUTER APPLY (SELECT tt.newname AS pNewName FROM t AS tt WHERE tt.id=t.[ParentID]) a
ORDER BY LEFT(t.[name],1),CASE WHEN t.layer=t.MaxLayer THEN ISNULL(a.pNewName,'') +convert(VARCHAR, t.[date],101) ELSE t.newname END
ID Name Date ParentID Layer newname MaxLayer pNewName (No column name)
1 A 2016-04-01 NULL 0 A-0-0 2 NULL 04/01/2016
2 A-1 2016-04-02 1 1 A-1-0 2 A-0-0 A-0-004/02/2016
4 A-1-2 2016-04-04 2 2 A-1-2 2 A-1-0 A-1-004/04/2016
3 A-1-1 2016-04-05 2 2 A-1-1 2 A-1-0 A-1-004/05/2016
5 A-2 2016-04-06 1 1 A-2-0 2 A-0-0 A-0-004/06/2016
6 B 2016-03-01 NULL 0 B-0 1 NULL 03/01/2016
8 B-2 2016-04-01 6 1 B-2 1 B-0 B-004/01/2016
7 B-1 2016-05-01 6 1 B-1 1 B-0 B-005/01/2016if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(25),[Date] Date,[ParentID] int,[Layer] int)
Insert #T
select 1,N'A','2016-4-1',null,0 union all
select 2,N'A-1','2016-4-2',1,1 union all
select 3,N'A-1-1','2016-4-5',2,2 union all
select 4,N'A-1-2','2016-4-4',2,2 union all
select 5,N'A-2','2016-4-6',1,1 union all
select 6,N'B','2016-3-1',null,0 union all
select 7,N'B-1','2016-5-1',6,1 union all
select 8,N'B-2','2016-4-1',6,1 union all
select 9,N'A-111111','2016-4-1',1,1 union all
select 10,N'A-111111-1','2016-5-1',9,2 union all
select 11,N'A-111111-2','2016-4-1',9,2
Go
with cnt as (
select COUNT(1) cnt from #T
)
,MaxLayer as (select MAX(Layer)MaxLayer from #T)
,cte as (
select
*
,POWER((select cnt from cnt),(select MaxLayer from MaxLayer)-Layer)*ROW_NUMBER()over(order by date)CurrSort
from #T
where Layer=0
union all
select
t.*
,cte.CurrSort+POWER((select cnt from cnt),(select MaxLayer from MaxLayer)-t.Layer)*ROW_NUMBER()over(order by t.date)
from cte join #T t on cte.ID=t.ParentID
)
select *
from cte
order by CurrSort
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(25),[Date] Date,[ParentID] int,[Layer] int)
Insert #T
select 1,N'A','2016-4-1',null,0 union all
select 2,N'A-1','2016-4-2',1,1 union all
select 3,N'A-1-1','2016-4-5',2,2 union all
select 4,N'A-1-2','2016-4-4',2,2 union all
select 5,N'A-2','2016-4-6',1,1 union all
select 6,N'B','2016-3-1',null,0 union all
select 7,N'B-1','2016-5-1',6,1 union all
select 8,N'B-2','2016-4-1',6,1
Go
SELECT a.*
FROM #T AS a
CROSS APPLY (SELECT TOP 1 Date,Name FROM #T WHERE a.Name LIKE name + '%' ORDER BY a.name) AS b
ORDER BY b.Date,b.Name,LEN(a.name),Date;
/*
ID Name Date ParentID Layer
6 B 2016-03-01 NULL 0
8 B-2 2016-04-01 6 1
7 B-1 2016-05-01 6 1
1 A 2016-04-01 NULL 0
2 A-1 2016-04-02 1 1
5 A-2 2016-04-06 1 1
4 A-1-2 2016-04-04 2 2
3 A-1-1 2016-04-05 2 2
*/