2013-04-23 13 views
6

I miei dati sono in tabella con 2 campi, Id & ParentId. Conservo i dati con questa struttura (immagine simile in seguito). Come posso ottenere tutto il percorso dalla foglia alla radice che include Id = 6? (Campione risultato è in seguito)Ottieni tutti gli alberi dal set di dati di righe (con ID specifico) in tsql

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

risposta

4

La tua tabella dice che 4 ha come genitore ma non nient'altro, tuttavia hai una riga che afferma che 12> 10> 6> 5> 4> 2> 1 quindi non posso produrre lo stesso risultato con quello impostare.

mia sqlfiddle per questo è qui: http://sqlfiddle.com/#!6/873b9/3

Assumendo 4 ha 2 come genitore il mio codice simile a questo (ordinamento potrebbe essere un po 'diverso, ma il suo SQL quindi è ok):

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) 
1

Per una tabella come this sample table, verificare questa query:

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%' 

Spero che questo aiuto!