添加包含cte的列,其中包含一个级别的父级

时间:2013-10-06 08:43:02

标签: sql sql-server sql-server-2008 common-table-expression recursive-query

我在视图中有以下代码,它运行良好并提供所需的输出。

;WITH ctesample 
     AS (SELECT sjob, 
                mjob, 
                mjob AS DisplayMJob, 
                groupf, 
                0    AS LVL 
         FROM   dbo.vw_jobcosting_wipmastersub 
         WHERE  ( groupf = 'True' ) 
         UNION ALL 
         SELECT vw_JobCosting_WIPMasterSub_1.sjob, 
                vw_JobCosting_WIPMasterSub_1.mjob, 
                CASE S.lvl 
                  WHEN 0 THEN S.sjob 
                  ELSE S.displaymjob 
                END       AS DisplayMJob, 
                vw_JobCosting_WIPMasterSub_1.groupf, 
                S.lvl + 1 AS Expr1 
         FROM   ctesample AS S 
                INNER JOIN dbo.vw_jobcosting_wipmastersub AS 
                           vw_JobCosting_WIPMasterSub_1 
                        ON S.sjob = vw_JobCosting_WIPMasterSub_1.mjob 
         WHERE  ( vw_JobCosting_WIPMasterSub_1.groupf = 'False' )) 
SELECT sjob, 
       CASE 
         WHEN displaymjob IS NULL THEN sjob 
         ELSE displaymjob 
       END AS MJob, 
       groupf 
FROM   ctesample AS cteSample_1 

我需要再添加一列,为每条记录提供输出MJob(不是sorce数据MJob)的父级。

源数据采用以下形式:

╔═════════════════════════╗
║ SJob,MJob,GroupF        ║
╠═════════════════════════╣
║ 10001000,NULL,True      ║
║ 10001001,NULL,True      ║
║ 10001002,NULL,True      ║
║ 20001000,10001000,True  ║
║ 20001001,10001001,True  ║
║ 20001002,10001001,True  ║
║ 20001003,10001002,True  ║
║ 30001000,20001000,False ║
║ 30001001,20001000,False ║
║ 30001002,20001001,False ║
║ 30001003,20001002,False ║
║ 30001004,20001002,False ║
║ 30001005,20001003,False ║
║ 30001006,20001003,False ║
║ 30001007,30001000,False ║
║ 30001008,30001000,False ║
║ 30001009,30001001,False ║
║ 30001010,30001002,False ║
║ 30001011,30001002,False ║
║ 30001012,30001003,False ║
║ 30001013,30001004,False ║
║ 30001014,30001005,False ║
║ 30001015,30001006,False ║
║ 30001016,30001006,False ║
╚═════════════════════════╝

我不知道如何处理它 建议将不胜感激。

所需的输出如下:

Sjob,Mjob,OutputMJob,GroupF
10001000,NULL,NULL,TRUE
10001001,NULL,NULL,TRUE
10001002,NULL,NULL,TRUE
20001000,10001000,NULL,TRUE
20001001,10001001,NULL,TRUE
20001002,10001001,NULL,TRUE
20001003,10001002,NULL,TRUE
30001000,20001000,10001000,FALSE
30001001,20001000,10001000,FALSE
30001002,20001001,10001001,FALSE
30001003,20001002,10001001,FALSE
30001004,20001002,10001001,FALSE
30001005,20001003,10001002,FALSE
30001006,20001003,10001002,FALSE
30001007,20001000,10001000,FALSE
30001008,20001000,10001000,FALSE
30001009,20001000,10001000,FALSE
30001010,20001001,10001001,FALSE
30001011,20001001,10001001,FALSE
30001012,20001002,10001001,FALSE
30001013,20001002,10001001,FALSE
30001014,20001003,10001002,FALSE
30001015,20001003,10001002,FALSE
30001016,20001003,10001002,FALSE

1 个答案:

答案 0 :(得分:0)

这个问题对我来说并不完全清楚,但据我所知,你想要这样的事情:

;WITH ctesample 
     AS (SELECT sjob, 
                mjob, 
                mjob AS DisplayMJob, 
                groupf, 
                0    AS LVL, 
                NULL outputMjob 
         FROM   dbo.vw_jobcosting_wipmastersub 
         WHERE  ( groupf = 'True' ) 
         UNION ALL 
         SELECT vw_JobCosting_WIPMasterSub_1.sjob, 
                vw_JobCosting_WIPMasterSub_1.mjob, 
                CASE S.lvl 
                  WHEN 0 THEN S.sjob 
                  ELSE S.displaymjob 
                END       AS DisplayMJob, 
                vw_JobCosting_WIPMasterSub_1.groupf, 
                S.lvl + 1 AS Expr1, 
                S.mjob 
         FROM   ctesample AS S 
                INNER JOIN dbo.vw_jobcosting_wipmastersub AS 
                           vw_JobCosting_WIPMasterSub_1 
                        ON S.sjob = vw_JobCosting_WIPMasterSub_1.mjob 
         WHERE  ( vw_JobCosting_WIPMasterSub_1.groupf = 'False' )) 
SELECT sjob, 
       CASE 
         WHEN displaymjob IS NULL THEN sjob 
         ELSE displaymjob 
       END AS MJob, 
       groupf, 
       outputmjob 
FROM   ctesample AS cteSample_1 

可以在SQL Fiddle找到一个工作示例 如果这不是你提出的问题,请告诉我,我会尽力给你一个更好的答案。