使用公用表表达式对Hierarchical表进行排序

时间:2012-03-19 08:34:12

标签: sql-server-2008

我在使用公用表表达式排序Hierarchical表时遇到困难。请看我的样本 我有一个名为 COMMON.TASK_REL

的表

enter image description here

我的第二张桌子叫普通。任务 enter image description here

我的SQL查询

                WITH ctLevel
                AS
                (
                    SELECT  
                        C_TASK_ID                                                               AS Child
                        ,P_Task_ID                                                              AS Parent
                        ,common_task.   TASK_SEQ                                                AS taskSeq
                        ,1                                                                      AS [Level]
                        ,CAST(C_TASK_ID AS VARCHAR(MAX))                                        AS [Order]
                        ,CAST (Replicate(' ', 1) + common_task.TASK_NAME AS VARCHAR(MAX))       AS [Task_Name]
                    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] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT' 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]
                        ,[Order] + '.' + CAST(C_TASK_ID AS VARCHAR(MAX))                            AS [Order]
                        ,CAST (Replicate(' ', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(MAX))  AS [Task_Name]
                    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] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT' AND common_task.[STATUS] <> 'D'
                    )


                    SELECT [Parent] ,[Child],taskSeq, Level,[Order],[Task_Name]as taskName
                    FROM   ctLevel  
                    order by [Order]

我的sql结果

enter image description here

我在这里得到一个问题。如何将 taskseq 排序为下面的结果

taskName      taskSeq
-Item1          1
--Item 1.1      1
--Item 1.2      2
--Item 1.3      3
-item 3         2
-Item 2         3
--Item 2.1      1
--Item 2.2      2
--Item 2.3
---Item 2.3.1

1 个答案:

答案 0 :(得分:1)

在我看来,你想通过taskSeq订购,但是孩子们有不同的价值观。解决这个问题的方法是引入另一个字段,它将父值的值复制到其所有子节点:

WITH ctLevel
AS
(
SELECT  
    C_TASK_ID                                                               AS Child
    ,P_Task_ID                                                              AS Parent
    ,common_task.TASK_SEQ                                                   AS taskSeq
    ,common_task.TASK_SEQ AS sorting_field
    ,1                                                                      AS [Level]
    ,CAST(C_TASK_ID AS VARCHAR(MAX))                                        AS [Order]
    ,CAST (Replicate(' ', 1) + common_task.TASK_NAME AS VARCHAR(MAX))       AS [Task_Name]
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] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT' 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
    ,ctLevel.sorting_field
    ,[Level] + 1                                                                AS [Level]
    ,[Order] + '.' + CAST(C_TASK_ID AS VARCHAR(MAX))                            AS [Order]
    ,CAST (Replicate(' ', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(MAX)) AS [Task_Name]
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] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT' AND common_task.[STATUS] <> 'D'
)

SELECT [Parent] ,[Child],taskSeq, Level,[Order],[Task_Name]as taskName
FROM   ctLevel  
order by sorting_field, [Order]