我正在使用 CTE 来管理我的层次结构数据。以下是我的选择查询
WITH ctLevel
AS
(
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task. TASK_SEQ AS taskSeq
,1 AS [Level]
,CAST( TASK_SEQ AS VARCHAR(MAX)) AS [taskOrder]
,CAST (Replicate(TASK_SEQ, 1)+' '+ TASK_NAME AS VARCHAR(MAX)) AS [Task_Name]
,common_task.TASK_POSITION AS [TASK_POSITION]
FROM
[COMMON.TASK_REL] as common_task_rel,
[COMMON.TASK] as common_task
WHERE
common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] ='PRODE' AND common_task.[STATUS] <> 'D'
and common_task_rel.P_TASK_ID is null
UNION ALL
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task. TASK_SEQ AS taskSeq
,[Level] + 1 AS [Level]
,[taskOrder] + '.' + CAST(TASK_SEQ AS VARCHAR(MAX)) AS [taskOrder]
,CAST (Replicate(' ', [Level] + 1) + taskOrder + '.' + CAST(TASK_SEQ AS VARCHAR(MAX))+' ' +common_task.TASK_NAME AS VARCHAR(MAX)) AS [Task_Name]
,common_task.TASK_POSITION AS [TASK_POSITION]
FROM
[COMMON.TASK_REL]as common_task_rel
INNER JOIN
ctLevel
ON
( P_Task_ID = Child ) ,
[COMMON.TASK] as common_task
WHERE
common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] = 'PRODE' AND common_task.[STATUS] <> 'D'
)
SELECT [Parent] ,[Child],taskSeq, Level,[taskOrder],Task_Name,[TASK_POSITION]
FROM ctLevel
order by [taskOrder],[Level],[TASK_POSITION]
我的结果如何:
你可以举个例子。假设
10 10.1 10.1.1 10.1.1.1将在 9 的底部
是否可以像下面那样对这些记录进行排序
1
2
2.1
2.2
2.4
2.5
2.6
2.7
3
3.1
3.2
4
4.1
4.2
5
5.1
6
6.1
6.2
6.3
6.4
6.5
7
8
9
9.1
9.1
9.1.1
9.1.1.1
9.1.1.2
9.1.1.3
9.1.1.4
10
10.1
10.1.1
10.1.1.1
提前致谢。