473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I write a query to get the path from ancestor to node in a tree?

Hi all. Here's my problem: I have a tree linking macaddresses (bigints)
in a tree structure. i want to get the path from node a to b.

create table tree1(father bigint , child bigint);

insert into tree1 (father,child) values (100,200);
insert into tree1 (father,child) values (100,300);
insert into tree1 (father,child) values (100,400);
insert into tree1 (father,child) values (200,2000);
insert into tree1 (father,child) values (200,3000);
insert into tree1 (father,child) values (100,4000);
insert into tree1 (father,child) values (2000,11111);

you can see that 100 --> 200 --> 2000 --> 11111

select * from tree1
what i would like is a query that given two parameters returns the path
between
them, in the case of 100,11111 i want to get
100
200
2000
11111

if possible as different rows, but columns will do to.

of course i do not know the legnth of the path. it can be very big

thx in advance
Tzvika

Sep 12 '05 #1
3 2234
Try that:

IF OBJECT_ID('Tree') IS NOT NULL
DROP Table Tree

CREATE TABLE Tree
(
Parent INT,
Child INT
)

INSERT INTO Tree
SELECT 100,200
INSERT INTO Tree
SELECT 100,300
INSERT INTO Tree
SELECT 100,400
INSERT INTO Tree
SELECT 400,500
INSERT INTO Tree
SELECT 500,1000

DROP FUNCTION getHierarchy

GO

CREATE FUNCTION getHierarchy
(
@child INT
)
RETURNS @Hierarchy TABLE
(
Path INT
)
AS
BEGIN

IF NOT EXISTS (SELECT * from Tree where child = @child)
BEGIN
INSERT INTO @Hierarchy
SELECT NULL
SET @child = NULL
END

WHILE (@Child IS NOT NULL OR LEN(@Child) = 0)
BEGIN
INSERT INTO @Hierarchy
SELECT @Child

SELECT @Child = Parent from Tree where child = @child
IF @@ROWCOUNT = 0 SET @Child = NULL
END

RETURN
END

GO

Select * from dbo.getHierarchy(500)

HTH, Jens Suesmeyer

---
http://www.sqlserver2005.de
---

Sep 12 '05 #2
For now, Google "nested sets model" for a better way. Then get a copy
of TREES & HIERARCHIES IN SQL for other ways to model this data.

Sep 12 '05 #3
Thanks Joe, Jens. Good stuff.
Thanks again.
Tzvika

Sep 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: per | last post by:
im not very good at sql but need to query the database to use in my programming script. if the database is just like this id name parent_id 1 A ...
5
by: John Smith | last post by:
Is it possible using an XPath query to select a child node and all its parents. e.g. <a x="1"> <b x="2"> <c x="3"/> <c x="4"/> </b> <b x="5"> <c x="6"/>
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
13
by: André Nogueira | last post by:
Hi there. I know you can view a node's fullpath property, but is it posible to select a node using its path? Like, tell the treeview that the node that should be selected is the node with the...
4
by: James L | last post by:
I have a tree view with a root, 3 noodes(1,2,3) each having one sub node. I drill down to a node. I get the path by saying treeView1.selectedNode.fullpath and assign it to a variable. If I...
2
by: James L | last post by:
I have finally developed some code that allows you to re populate a tree view and re select the last node that was clicked. However, you have to hard code it for the number of levels the tree view...
30
by: Vadim Tropashko | last post by:
Reposting with more clarification (as Jan asked). Suppose I have a BNFgrammar and a source text parsed into a tree. How would I query an identifier declaration? All the XQuery tutorials...
0
by: Pivot_Tables | last post by:
Please follow the below link as this post is continuation from the below post. http://groups.google.com/group/comp.databases.ibm-db2/browse_thread/thread/1589589ccf38eb0a Hi Guys, I got...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.