Wednesday, March 7, 2012

hiearchical list of tree

Hi,

table(id,parent,level, name)
parent ...fk referenced table(id))
level ... depth of tree (root has 0)

How SQL for hiearchical list:

name 1
name 1.1
name 1.2
name 2.
name 2.1.
name 2.1.1
name 2.1.2
etc.

?
thanks for your helpPlease post the complete DDL for your table, some sample data, and the desired result. That, I might be able to help with.|||create table A(
id int primary key not null,
parent int null, --parent of the row
level int null, --position in DB tree from root
name varchar (20)
)

alter table A add
constraint fk foreign key(parent) references A(id)

insert into A(id,parent,level,name) values (1,null,0,'name 1.');
insert into A(id,parent,level,name) values (2,1,1,'name 1.1.');
insert into A(id,parent,level,name) values (3,1,1,'name 1.3.');
insert into A(id,parent,level,name) values (4,2,2,'name 1.1.1.');
insert into A(id,parent,level,name) values (5,1,1,'name 1.2.');
insert into A(id,parent,level,name) values (6,3,2,'name 1.3.1.');
...

I'd like to display :
name 1.
name 1.1.1.
name 1.2.
name 1.3.
name 1.3.1.
...

name 1.
name 1.|||this will do it...select name from A order by name
i know it's not the general solution, but it is a prefectly good solution to the particular data that you have, with the enumerated path included as part of the name|||thanks, but else, not order name
Column a.name contains 'sdfdasasd' etc too.|||okay, see Recursive Queries in SQL Server 2005 (http://www.sqlservercentral.com/columnists/sSampath/recursivequeriesinsqlserver2005.asp)|||I'd like to avoid a recursion solution. How ways are possible?
DDL can be altered.|||how deep is the tree? how many levels?|||more than 15 levels|||my condolences, as that would be phenomenally difficult for most users to navigate|||I'd like to avoid a recursion solution.
Why? Recursion would be the easiest method, especially if the number of levels isn't fixed.|||because a recursion should be time difficult|||you may not want to hear this, but any solution for the adjacency model going down 15+ levels will be "time difficult"

have you thought about a different data design?

do a search for joe celko's nested set model

No comments:

Post a Comment