如何在SQL Server中简化此SQL查询?
我正在尝试获取父子层次结构。这是我的查询,如果有任何方法可以简化它…仅使用此查询如何在SQL Server中简化此SQL查询?,sql,sql-server,Sql,Sql Server,我正在尝试获取父子层次结构。这是我的查询,如果有任何方法可以简化它…仅使用此查询 SELECT Chain0.ParId AS ParentId, CASE WHEN Chain3.title <> ' ' THEN Chain3.title + ' > ' + Chain2.title + ' > ' + Chain1.title + ' > ' + Chain0.title WHEN Cha
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
输出:
ParentId title
---------------------------
NULL test
1 test > get
2 test > get > this
3 test > get > this > value
简化这个查询我需要像上面这样的格式输出…这是通过
这是通过
您可以共享表结构和一些示例数据吗?id title 1 test 2 get 3 this 4 value id text parId 1 test1 null 2 get1 3 this1 2 4 value1 3请不要将代码示例或示例数据放入注释中-因为您无法格式化它,所以读取它非常困难。。。。取而代之的是:通过编辑你的问题来更新它,以提供额外的信息!谢谢。示例中有一个输入错误:它必须是“TestTable2 as T2”在整个查询过程中。您可以共享表结构和一些示例数据吗?id title 1 test 2 get 3 this 4 value id text parId 1 test1 null 2 get1 1 3 this 1 2 4 value1 3请不要将代码示例或示例数据放入注释中-因为您无法格式化它,所以读取它非常困难。。。。取而代之的是:通过编辑你的问题来更新它,以提供额外的信息!谢谢。示例中有一个输入错误:在整个查询过程中,它必须是“TestTable2 as T2”。很有可能,尽管不知道为什么?那么您真的应该使用递归。当然,您可以根据需要复制最后一个查询中的行。使用相应的Ref5、6和7来访问Src5、6和7是非常简单的。这是很可能的,尽管不知道为什么?那么您真的应该使用递归。当然,您可以根据需要复制最后一个查询中的行。通过相应的参考文献5、6和7,可以非常直接地访问Src5、6和7。
ParentId title
---------------------------
NULL test
1 test > get
2 test > get > this
3 test > get > this > value
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
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