942,779 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 7165
  • MS SQL RSS
Mar 17th, 2008
0

Depth Travesal

Expand Post »
Hi, I am a newbie for MSSQL. Previously, I was given a question regarding MSSQL. Although the interview had over, but I am curious to find out on how to solve 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snow00 is offline Offline
2 posts
since Mar 2008
Mar 20th, 2008
0

Re: Depth Travesal

Try this code in Query Analyser. It enough for small data. If you want to use for big data, modify the code.

MSSQL Syntax (Toggle Plain Text)
  1. SET nocount ON
  2. CREATE TABLE #tmpName (ID int, [Name] varchar(255))
  3. CREATE TABLE #tmpRelation (ID int, ID_Parent int)
  4.  
  5. INSERT #tmpName (ID, [Name]) select 1, 'Frank'
  6. INSERT #tmpName (ID, [Name]) select 2, 'Jo'
  7. INSERT #tmpName (ID, [Name]) select 3, 'Mary'
  8. INSERT #tmpName (ID, [Name]) select 4, 'Peter'
  9. INSERT #tmpName (ID, [Name]) select 5, 'Amy'
  10.  
  11. INSERT #tmpRelation (ID, ID_Parent) select 1, 0
  12. INSERT #tmpRelation (ID, ID_Parent) select 2, 1
  13. INSERT #tmpRelation (ID, ID_Parent) select 3, 2
  14. INSERT #tmpRelation (ID, ID_Parent) select 4, 1
  15. INSERT #tmpRelation (ID, ID_Parent) select 5, 2
  16.  
  17. CREATE TABLE #tmpResult (ID int, ID_Parent int, [Level] int, [Name] varchar(255))
  18.  
  19. INSERT #tmpResult (ID, ID_Parent, [Level], [Name])
  20. SELECT N.ID, R.ID_Parent, 0, N.[Name]
  21. FROM #tmpName N inner join #tmpRelation R on
  22. N.ID = R.ID
  23. WHERE ID_Parent = 0
  24.  
  25. while EXISTS (SELECT 1 FROM #tmpName N left join #tmpResult R on N.ID = R.ID where R.ID is null)
  26. begin
  27. INSERT #tmpResult (ID, ID_Parent, [Level], [Name])
  28. SELECT N.ID, R.ID_Parent, C.[Level] + 1, N.[Name]
  29. FROM #tmpName N inner join #tmpRelation R on
  30. N.ID = R.ID INNER JOIN (
  31. SELECT P.ID, P.[Level]
  32. FROM #tmpResult P left join #tmpResult C on
  33. P.ID = C.ID_Parent
  34. WHERE C.ID IS NULL
  35. ) C ON
  36. R.ID_Parent = C.ID
  37. end
  38.  
  39. SELECT ID, [Level], Name FROM #tmpResult
  40.  
  41. DROP TABLE #tmpResult
  42. DROP TABLE #tmpName
  43. DROP TABLE #tmpRelation
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008
Mar 23rd, 2008
0

Re: Depth Travesal

Thanks Huangzi, will test it later. Thanks again for the solution you offer.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snow00 is offline Offline
2 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Looking for help setting up a DB for phpbb3
Next Thread in MS SQL Forum Timeline: help in performing join





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC