我正在尝试将父级转换为子级别。这是我的查询,如果有任何简化方法......仅使用此查询
SELECT
Chain0.ParId AS ParentId,
CASE
WHEN Chain3.title <> ' '
THEN Chain3.title + ' > ' + Chain2.title + ' > ' + Chain1.title + ' > ' + Chain0.title
WHEN Chain2.title <> ' '
THEN Chain2.title + ' > ' + Chain1.title + ' > ' + Chain0.title
WHEN Chain1.title <> ''
THEN Chain1.title + ' > ' + Chain0.title
WHEN Chain0.title <> ' '
THEN Chain0.title
END AS title
FROM
(SELECT
T1.Id, T1.Title, T2.ParId
FROM
TestTable as T1
LEFT OUTER JOIN
TestTable2 as T2 ON T1.Id = T2.Id) Chain0
LEFT OUTER JOIN
(SELECT
T1.Id, T1.Title, T2.ParId
FROM
TestTable as T1
LEFT OUTER JOIN
TestTable as T2 ON T1.Id = T2.Id) Chain1 ON Chain0.ParId = Chain1.Id
LEFT OUTER JOIN
(SELECT
T1.Id, T1.Title, T2.ParId
FROM
TestTable as T1
LEFT OUTER JOIN
TestTable as T2 ON T1.Id = T2.Id) Chain2 ON Chain1.ParId = Chain2.Id
LEFT OUTER JOIN
(SELECT
T1.Id, T1.Title, T2.ParId
FROM
TestTable as T1
LEFT OUTER JOIN
TestTable as T2 ON T1.Id = T2.Id) Chain3 ON Chain2.ParId = Chain3.Id
LEFT OUTER JOIN
(SELECT
T1.Id, T1.Title, T2.ParId
FROM
TestTable as T1
LEFT OUTER JOIN
TestTable as T2 ON T1.Id = T2.Id) Chain4 ON Chain3.ParId = Chain4.Id
这是我的两个表testtable和testTable2 ......
id title
1 test
2 get
3 this
4 value
id text parId
1 test1 null
2 get1 1
3 this1 2
4 value1 3
输出:
ParentId title
---------------------------
NULL test
1 test > get
2 test > get > this
3 test > get > this > value
简化此查询我需要输出如上格式...
答案 0 :(得分:2)
这是通过Recursive CTE
完成的With
TestTable (id, title) As (
Select 1, 'test' Union All
Select 2, 'get' Union All
Select 3 ,'this' Union All
Select 4, 'value'
),
TestTable2 (id, text, parId) As (
Select 1, 'test1' , null Union All
Select 2, 'get1' , 1 Union All
Select 3, 'this1' , 2 Union All
Select 4, 'value1', 3
),
RecursiveCTE As (
Select
Src.id,
Ref.parId,
CAST(Src.title As varchar(1024)) As title --< Types in the Union must be aligned
From TestTable Src
Inner Join TestTable2 Ref On Src.id = Ref.id AND Ref.parId Is NULL
Union All
Select
Src.id,
Ref.parId,
CAST(Prev.title + ' > ' + Src.title As varchar(1024)) As title --< ... aligned
From TestTable Src
Inner Join TestTable2 Ref On Src.id = Ref.id
Inner Join RecursiveCTE Prev On Prev.id = Ref.parId --< Recursive call to self
)
Select parId As ParentId, title From RecursiveCTE
更新:有足够的Recirsive CTE,它可以是:
With
TestTable (id, title) As (
Select 1, 'test' Union All
Select 2, 'get' Union All
Select 3 ,'this' Union All
Select 4, 'value'
),
TestTable2 (id, text, parId) As (
Select 1, 'test1' , null Union All
Select 2, 'get1' , 1 Union All
Select 3, 'this1' , 2 Union All
Select 4, 'value1', 3
)
Select
Ref1.parID As ParentId,
COALESCE(Src4.title + ' > ', '') +
COALESCE(Src3.title + ' > ', '') +
COALESCE(Src2.title + ' > ', '') +
Src1.title As title
From TestTable2 Ref1
Left Join TestTable2 Ref2 On Ref2.id = Ref1.parId
Left Join TestTable2 Ref3 On Ref3.id = Ref2.parId
Left Join TestTable2 Ref4 On Ref4.id = Ref3.parId
Left Join TestTable Src1 On Src1.id = Ref1.id
Left Join TestTable Src2 On Src2.id = Ref2.id
Left Join TestTable Src3 On Src3.id = Ref3.id
Left Join TestTable Src4 On Src4.id = Ref4.id