添加包含上一级父级的 cte 列
Posted
技术标签:
【中文标题】添加包含上一级父级的 cte 列【英文标题】:Add column with cte containing parent of one level up 【发布时间】:2013-10-12 23:22:29 【问题描述】:我在 View 中有以下代码,它运行良好并提供所需的输出。
;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(而不是源数据 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 的父级”是什么意思? 【参考方案1】:这个问题对我来说并不完全清楚,但据我了解,你想要这样的东西:
;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 上找到一个工作示例。 如果这不是您所问的问题,请告诉我,我会尽力为您提供更好的答案。
【讨论】:
对不起,我不是很清楚。这接近我需要的,但并不完全。使用您的示例,OutputMJob 应该始终是 MJob 的父级。 那么你能尝试添加一些细节以便清楚吗? 我在表达我需要的东西时遇到了一点麻烦,我模拟了我所追求的输出。我希望这能让它更清楚一点。 @user2849560,在哪里模拟出来的? 抱歉,发到早了。在这里。以上是关于添加包含上一级父级的 cte 列的主要内容,如果未能解决你的问题,请参考以下文章