473,320 Members | 1,846 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,320 software developers and data experts.

how to query this table into the result like this?

per

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 null <----- root
2 B 1
3 C 1
4 D 1
5 E 2
6 F 2
7 G 3
8 H 4
9 I 4
10 J 4
11 K 9

the data in the above table is just like some sort of tree data
which have parent and child node
and now,how to query into the result like this

the deepest node is in level 4

what i want is how deep this tree data is?
anyone show me the query script or store procedure to find the
result please?
many thanks in advance,

--
Posted via http://dbforums.com
Jul 20 '05 #1
1 3643
"per" <me*********@dbforums.com> wrote in message news:31****************@dbforums.com...

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 null <----- root
2 B 1
3 C 1
4 D 1
5 E 2
6 F 2
7 G 3
8 H 4
9 I 4
10 J 4
11 K 9

the data in the above table is just like some sort of tree data
which have parent and child node
and now,how to query into the result like this

the deepest node is in level 4

what i want is how deep this tree data is?
anyone show me the query script or store procedure to find the
result please?
many thanks in advance,

--
Posted via http://dbforums.com


CREATE TABLE Tree
(
node_id INT NOT NULL PRIMARY KEY,
node_name VARCHAR(10) NOT NULL UNIQUE,
parent_id INT NULL REFERENCES Tree (node_id)
)

-- Sample data
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (1, 'A', NULL)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (2, 'B', 1)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (3, 'C', 1)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (4, 'D', 1)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (5, 'E', 2)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (6, 'F', 2)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (7, 'G', 3)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (8, 'H', 4)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (9, 'I', 4)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (10, 'J', 4)
INSERT INTO Tree (node_id, node_name, parent_id)
VALUES (11, 'K', 9)

-- Returns a table of all descendants of a given node and their level numbers
-- If the given node is NULL, use root node of tree
CREATE FUNCTION Descendants(@root_node VARCHAR(10) = NULL)
RETURNS @nodes TABLE
(node_id INT NOT NULL PRIMARY KEY,
level_number INT NOT NULL CHECK (level_number >= 0))
AS
BEGIN
IF @root_node IS NULL
SELECT @root_node = node_name
FROM Tree
WHERE parent_id IS NULL
IF NOT EXISTS (SELECT * FROM Tree WHERE node_name = @root_node)
RETURN
DECLARE @level_number INT,
@next_level_number INT
SELECT @level_number = 0,
@next_level_number = 1
INSERT INTO @nodes (node_id, level_number)
SELECT node_id, @level_number
FROM Tree
WHERE node_name = @root_node
WHILE EXISTS (SELECT * FROM @nodes
WHERE level_number = @level_number)
BEGIN
INSERT INTO @nodes (node_id, level_number)
SELECT T.node_id, @next_level_number
FROM @nodes AS N
INNER JOIN
Tree AS T
ON N.level_number = @level_number AND
N.node_id = T.parent_id
SELECT @level_number = @next_level_number,
@next_level_number = @next_level_number + 1
END
RETURN
END

CREATE VIEW TreeDepth (depth)
AS
SELECT COALESCE(MAX(level_number) + 1, 0)
FROM Descendants(DEFAULT)

SELECT depth
FROM TreeDepth

depth
4

To find the depth of the tree rooted at other than the tree's root node,
e.g., the depth of the tree rooted at node C, one can do the following

SELECT COALESCE(MAX(level_number) + 1, 0) AS depth
FROM Descendants('C')

depth
2

Regards,
jag
Jul 20 '05 #2

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

Similar topics

2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Nick Truscott | last post by:
<? // scoreinput.php - input a match score when match selected from list ?> <html> <head> <basefont face="Verdana"> </head> <body>
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
2
by: Maxi | last post by:
I have 101 fields (Field name P1, P2 .... P100 and Result) P1 to P100 has random numbers from 1 to 10. I want to run a query to update the 101st field (Result). It should check the entire record...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
4
by: frizzle | last post by:
Hi there, Still building my forum. I have a certain mysql-query i just can't figure out. These are my tables (simplified): categories -> id, description forums -> id, cat_id,...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.