我在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
你的數據庫結構中的「輸出MJob的父親」是什麼意思? – Szymon