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...
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...