sql >> Database teknologi >  >> RDS >> Sqlserver

Sådan får du forælderen givet et barn i SQL SERVER 2005

Jeg synes, du skal omdøbe dit child_id til node, dit parent_id til child_of. Dit kolonnenavn er lidt forvirrende

create table stack_overflow
(
node int, child_of int
);


insert into stack_overflow(node, child_of) values
(1,0),
(2,1),
(3,2),
(4,2),
(5,3),
(6,4),
(7,0),
(8,7),
(9,8),
(10,1);

Dette virker på alle CTE-kompatible RDBMS :

with find_parent(parent, child_of, recentness) as
(
    select node, child_of, 0 
    from stack_overflow
    where node = 9
    union all
    select i.node, i.child_of, fp.recentness + 1
    from stack_overflow i
    join find_parent fp on i.node = fp.child_of
)
select top 1 parent from find_parent 
order by recentness desc

Output:

parent
7

[EDIT:mere fleksibel og fremtidssikret] :

with find_parent(node_group, parent, child_of, recentness) as
(
    select node, node, child_of, 0
    from stack_overflow
    where node in (5,9)
    union all
    select fp.node_group, i.node, i.child_of, fp.recentness + 1
    from stack_overflow i
    join find_parent fp on i.node = fp.child_of
)
select q.node_group as to_find, parent as found 
from find_parent q 
join
(
    select node_group, max(recentness) as answer
    from find_parent
    group by node_group 
) as ans on q.node_group = ans.node_group and q.recentness = ans.answer 
order by to_find    

Output:

to_find     found
5           1
9           7

Hvis du bruger Postgres , kunne ovenstående kode forkortes til:

with recursive find_parent(node_group, parent, child_of, recentness) as
(
    select node, node, child_of, 0
    from stack_overflow
    where node in (5,9)
    union all
    select fp.node_group, i.node, i.child_of, fp.recentness + 1
    from stack_overflow i
    join find_parent fp on i.node = fp.child_of
)
select distinct on (node_group) node_group as to_find, parent as found 
from find_parent 
order by to_find, recentness desc

UNDSKEL PÅ sten! :-)



  1. Rails 3.2 + MySQL:Fejl:Feltet 'created_at' har ikke en standardværdi:INSERT INTO

  2. Hvad er nyt i PgBouncer 1.6

  3. Hvordan retter du en fejl i MySQL-forkert nøglefil, når du ikke kan reparere bordet?

  4. Svarende til PostgreSQL's array_agg i Oracle XE 11.2