By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

Table Relationship Query

P: 1
Hi all, anybody able to offer me some solution for the question below.

There are 2 tables, NAMES (ID INT IDENTITY(1,1), NAME SYSNAME) and
RELATIONSHIPS (NAMEID INT, PARENT_NAMEID INT) linked via NAMES.ID=
RELANTIONSHIP.NAMEID and where top-most name has a PARENT_NAMEID=0.
Show a nested list of names including LEVEL, NAMEID and NAME, where
LEVEL indicates the nest level (or depth) from top. You may use functions,
stored procedures,views and any other Transact SQL commands compliant
with Microsoft SQL 2000.


Sample Data:
NAMES table content

ID NAME
1 Frank
2 Jo
3 Mary
4 Peter
5 Amy

RELATIONSHIPS table content
NAMEID PARENT_NAMEID
1 0
2 1
3 2
4 1
5 2

Expected Output
LEVEL ID NAME
0 1 Frank
1 2 Jo
2 5 Amy
2 3 Mary
1 4 Peter

Thanks in advanced
Mar 15 '08 #1
Share this Question
Share on Google+
2 Replies


DrBunchman
Expert 100+
P: 979
So basically you want some help with your homework, huh?
Mar 17 '08 #2

ck9663
Expert 2.5K+
P: 2,878
So basically you want some help with your homework, huh?

I hope for, his sake, you're not his teacher :)
Mar 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.