CTE 递归语句与自连接没有 ids
CTE Recursion statement with self join without ids
我必须拆分 数据 列并与员工和经理建立关系。
Table ManagerDetails:
data
employee
manager
imp/imp2/imp3/imp4
imp2
notimp4
notimp1/notimp2/notimp3/notimp4
imp3
imp4
如果你观察下面的输出table,如果我取第一行imp4
是imp3
的经理,imp3
是[=的经理14=],imp2
是 imp
的经理,所以我需要构建 table,如下所述。
第一行的预期输出:
data
employee
manager
imp/imp2/imp3/imp4
imp4
imp/imp2/imp3
imp4
imp3
imp/imp2
imp3
imp2
imp
imp2
imp
我使用 CTE 以 的引用拆分 ManagerDetails
行并进行查询
DECLARE @separator varchar(1) = '/';
WITH rCTE AS(
SELECT
1 as [level] ,
d.data,
CHARINDEX(@separator, CONCAT(d.data, @separator)) AS Position
FROM ManagerDetails d
UNION ALL
SELECT
r.[level] + 1,
r.data,
CHARINDEX(@separator, CONCAT(r.data, @separator), r.Position + 1) AS Position
FROM rCTE r
WHERE CHARINDEX(@separator, CONCAT(r.data, @separator), r.Position + 1) > 0
)
SELECT [level], SUBSTRING(CONCAT(data, @separator), 1, Position - 1) AS [Value]
FROM rCTE
ORDER BY [level], Position DESC
OPTION (MAXRECURSION 0)
通过上述查询,我能够按预期显示数据列,但无法按预期更改经理和员工列。
感谢任何形式的帮助。
如果我理解正确,你只需要一个 LAG()
和分隔符在递归 CTE 中的位置:
Table:
CREATE TABLE ManagerDetails (id int, data varchar(1000))
INSERT INTO ManagerDetails (id, data)
VALUES
(1, 'imp/imp2/imp3/imp4'),
(2, 'notimp1/notimp2/notimp3/notimp4')
声明:
DECLARE @separator varchar(1) = '/';
WITH rCTE AS(
SELECT
d.id,
1 as [level] ,
d.data,
CAST(1 AS int) AS index1,
CHARINDEX(@separator, CONCAT(d.data, @separator)) AS index2
FROM ManagerDetails d
UNION ALL
SELECT
r.id,
r.[level] + 1,
r.data,
CAST(r.index2 + LEN(@separator) AS int),
CHARINDEX(@separator, CONCAT(r.data, @separator), r.index2 + 1)
FROM rCTE r
WHERE CHARINDEX(@separator, CONCAT(r.data, @separator), r.index2 + 1) > 0
)
SELECT
id AS Id,
SUBSTRING(CONCAT(data, @separator), 1, index2 - 1) AS [Value],
LAG(SUBSTRING(CONCAT(data, @separator), index1, index2 - index1)) OVER (PARTITION BY id ORDER BY level DESC) AS [Manager],
SUBSTRING(CONCAT(data, @separator), index1, index2 - index1) AS [Employee]
FROM rCTE
ORDER BY id, level DESC
OPTION (MAXRECURSION 0)
结果:
Id Value Manager Employee
---------------------------------------------------
1 imp/imp2/imp3/imp4 imp4
1 imp/imp2/imp3 imp4 imp3
1 imp/imp2 imp3 imp2
1 imp imp2 imp
2 notimp1/notimp2/notimp3/notimp4 notimp4
2 notimp1/notimp2/notimp3 notimp4 notimp3
2 notimp1/notimp2 notimp3 notimp2
2 notimp1 notimp2 notimp1
我必须拆分 数据 列并与员工和经理建立关系。
Table ManagerDetails:
data | employee | manager |
---|---|---|
imp/imp2/imp3/imp4 | imp2 | notimp4 |
notimp1/notimp2/notimp3/notimp4 | imp3 | imp4 |
如果你观察下面的输出table,如果我取第一行imp4
是imp3
的经理,imp3
是[=的经理14=],imp2
是 imp
的经理,所以我需要构建 table,如下所述。
第一行的预期输出:
data | employee | manager |
---|---|---|
imp/imp2/imp3/imp4 | imp4 | |
imp/imp2/imp3 | imp4 | imp3 |
imp/imp2 | imp3 | imp2 |
imp | imp2 | imp |
我使用 CTE 以 ManagerDetails
行并进行查询
DECLARE @separator varchar(1) = '/';
WITH rCTE AS(
SELECT
1 as [level] ,
d.data,
CHARINDEX(@separator, CONCAT(d.data, @separator)) AS Position
FROM ManagerDetails d
UNION ALL
SELECT
r.[level] + 1,
r.data,
CHARINDEX(@separator, CONCAT(r.data, @separator), r.Position + 1) AS Position
FROM rCTE r
WHERE CHARINDEX(@separator, CONCAT(r.data, @separator), r.Position + 1) > 0
)
SELECT [level], SUBSTRING(CONCAT(data, @separator), 1, Position - 1) AS [Value]
FROM rCTE
ORDER BY [level], Position DESC
OPTION (MAXRECURSION 0)
通过上述查询,我能够按预期显示数据列,但无法按预期更改经理和员工列。
感谢任何形式的帮助。
如果我理解正确,你只需要一个 LAG()
和分隔符在递归 CTE 中的位置:
Table:
CREATE TABLE ManagerDetails (id int, data varchar(1000))
INSERT INTO ManagerDetails (id, data)
VALUES
(1, 'imp/imp2/imp3/imp4'),
(2, 'notimp1/notimp2/notimp3/notimp4')
声明:
DECLARE @separator varchar(1) = '/';
WITH rCTE AS(
SELECT
d.id,
1 as [level] ,
d.data,
CAST(1 AS int) AS index1,
CHARINDEX(@separator, CONCAT(d.data, @separator)) AS index2
FROM ManagerDetails d
UNION ALL
SELECT
r.id,
r.[level] + 1,
r.data,
CAST(r.index2 + LEN(@separator) AS int),
CHARINDEX(@separator, CONCAT(r.data, @separator), r.index2 + 1)
FROM rCTE r
WHERE CHARINDEX(@separator, CONCAT(r.data, @separator), r.index2 + 1) > 0
)
SELECT
id AS Id,
SUBSTRING(CONCAT(data, @separator), 1, index2 - 1) AS [Value],
LAG(SUBSTRING(CONCAT(data, @separator), index1, index2 - index1)) OVER (PARTITION BY id ORDER BY level DESC) AS [Manager],
SUBSTRING(CONCAT(data, @separator), index1, index2 - index1) AS [Employee]
FROM rCTE
ORDER BY id, level DESC
OPTION (MAXRECURSION 0)
结果:
Id Value Manager Employee
---------------------------------------------------
1 imp/imp2/imp3/imp4 imp4
1 imp/imp2/imp3 imp4 imp3
1 imp/imp2 imp3 imp2
1 imp imp2 imp
2 notimp1/notimp2/notimp3/notimp4 notimp4
2 notimp1/notimp2/notimp3 notimp4 notimp3
2 notimp1/notimp2 notimp3 notimp2
2 notimp1 notimp2 notimp1