简体   繁体   English

如何在SQL Server中简化此SQL查询?

[英]How to simplify this SQL query in SQL Server?

I am trying to get parent to child hierarchy. 我正在尝试让子层次成为父级。 This is my query if any way to simplify this ...using this query only 这是我的查询,是否有任何简化方法...仅使用此查询

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   

this my two tables testtable and testTable2... 这是我的两个表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

Output: 输出:

ParentId    title
---------------------------
NULL        test
  1         test > get
  2         test > get > this
  3         test > get > this > value

Simplify this query I need output like this above format... 简化此查询,我需要上述格式的输出...

This is done through Recursive CTE 这是通过递归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

Update : withough Recirsive CTE it can be: 更新 :尽管具有递归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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM