User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 375,195 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,099 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 810 | Replies: 2
Reply
Join Date: Mar 2008
Posts: 2
Reputation: snow00 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
snow00 snow00 is offline Offline
Newbie Poster

Depth Travesal

  #1  
Mar 17th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 14
Reputation: huangzhi is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
huangzhi huangzhi is offline Offline
Newbie Poster

Solution Re: Depth Travesal

  #2  
Mar 19th, 2008
Try this code in Query Analyser. It enough for small data. If you want to use for big data, modify the code.

  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
Hence Wijaya
www.ex-Soft.tk
Reply With Quote  
Join Date: Mar 2008
Posts: 2
Reputation: snow00 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
snow00 snow00 is offline Offline
Newbie Poster

Re: Depth Travesal

  #3  
Mar 23rd, 2008
Thanks Huangzi, will test it later. Thanks again for the solution you offer.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 2:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC