6

My data is in table with 2 field, Id & ParentId. I store data with this structure(similar image in follow). How can I get all path from leaf to root that include Id = 6 ? (Result sample is in follow)

--Data structure is as follow :
--  1
-- /
--2 <- 3       9
-- \    \    / 
--  4 <- 5  7  8
--    \  /  /  /
--      6 - - -
--   /    \
--  10  <- 11
-- /
--12

--Data In Table Is :
--Id    ParentId
--1     null
--2     1
--3     2
--4     2
--5     3
--5     4
--6     4
--6     5
--6     7
--6     8
--7     9
--8     null
--9     null
--10    6
--11    6
--11    10
--12    10

--Result for all trees that include "Id = 6":
--12 > 10 > 6 > 4 > 2 > 1
--12 > 10 > 6 > 5 > 4 > 2 > 1
--12 > 10 > 6 > 5 > 3 > 2 > 1
--12 > 10 > 6 > 7 > 9
--12 > 10 > 6 > 8
--11 > 10 > 6 > 4 > 2 > 1
--11 > 10 > 6 > 5 > 4 > 2 > 1
--11 > 10 > 6 > 5 > 3 > 2 > 1
--11 > 10 > 6 > 7 > 9
--11 > 10 > 6 > 8
--11 > 6 > 4 > 2 > 1
--11 > 6 > 5 > 4 > 2 > 1
--11 > 6 > 5 > 3 > 2 > 1
--11 > 6 > 7 > 9
--11 > 6 > 8
4

2 回答 2

4

你的表说 4 有自己作为父母,但没有其他任何东西,但你有一行表明 12 > 10 > 6 > 5 > 4 > 2 > 1 所以我不能用那个设置产生相同的结果。

我的 sqlfiddle 在这里:http ://sqlfiddle.com/#!6/873b9/3

假设 4 有 2 作为父代码,我的代码如下所示(排序可能有点不同,但它的 SQL 所以没关系):

WITH records as
(
  SELECT
  leaf.Id
  ,leaf.ParentId
  ,case when NOT EXISTS(SELECT * FROM recTest where ParentId = leaf.Id) then 1 else 0 end as isLeaf
  FROM recTest as leaf
)
,hierarchy as
(
  SELECT Id
  ,NULL as ParentId
  ,cast(Id as varchar(100)) as chain
  ,isLeaf
  FROM records
  where ParentId IS NULL
  UNION ALL
  SELECT r.Id
  ,r.ParentId
  ,cast(cast(r.Id as varchar(100)) + ' > ' + h.chain as varchar(100)) as chain
  ,r.isLeaf
  FROM records as r
    INNER JOIN hierarchy as h
      ON r.ParentId = h.Id
)
SELECT
h.chain
FROM hierarchy as h
WHERE isLeaf = 1
AND h.chain like '%6%'
OPTION (MAXRECURSION 0)
于 2013-04-23T11:13:55.690 回答
1

对于像这个示例表这样的表,请检查以下查询:

with AllPossiblePath as(
SELECT distinct [descendant] leaf
   ,(
        SELECT cast(f.dirname as nvarchar(64) )+'/' 
        FROM filesystem f JOIN tree_path t 
        ON t.ancestor = f.id 
        WHERE t.descendant=t1.descendant for xml path('') 
    ) possiblePath
  FROM [db1].[dbo].[tree_path] t1
  where [descendant] not in(
  SELECT TOP 1000 ancestor
  FROM [db1].[dbo].[tree_path] 
  where ancestor!=[descendant])
  )

select * from AllPossiblePath where possiblePath like '%Dir2%'

希望这有帮助!

于 2013-04-23T09:50:04.670 回答