2

我在View中具有以下代碼,可以很好地工作並提供所需的輸出。添加包含父級的cte的列

;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 
+0

你的數據庫結構中的「輸出MJob的父親」是什麼意思? – Szymon

回答

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
請讓我知道,如果這不是你所問的,我會盡力給你一個更好的答案。

+0

對不起,我不是很清楚。這接近我所需要的,但並不完全。使用你的例子,OutputMJob應該總是MJob的父類。 – user2849560

+0

那麼你可以嘗試添加一些細節,以便清楚嗎? – Gidil

+0

我在表達我所需要的東西時有點麻煩,嘲笑了我之後的輸出。我希望這可以讓它更清楚一些。 – user2849560