Re: Reflexive table problem - tricky
From: james (nospam_at_hypercon.net)
Date: 10/14/04
- Next message: Tod: "RE: Access SQL to T-SQL"
- Previous message: JJ: "Re: Why am I getting this syntax error calling a SP?"
- In reply to: Joe Celko: "Re: Reflexive table problem - tricky"
- Next in thread: Joe Celko: "Re: Reflexive table problem - tricky"
- Reply: Joe Celko: "Re: Reflexive table problem - tricky"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 14 Oct 2004 10:50:18 -0500
Joe, the only problem here is that I provided my setup for you but I do not
have .lft and .rgt so I am not quite sure what you are telling me to do. If
you are telling me to re-design my entire table layout it isn't that easy as
we have applications accessing the data that I cannot mess with. When I
read your paper on the left right set way of modelling trees it looked as
though you had to design your table that way or am I not understanding it at
all. Bear with me here as I am not really an SQL guru, I am more of an
intermediate dabbler ;-)
JIM
"Joe Celko" <jcelko212@earthlink.net> wrote in message
news:OwOfYCZsEHA.2536@TK2MSFTNGP11.phx.gbl...
>>> have a standard Reflexive table and I need a way to take a given
> sub-tree within the table and flatten it out to a Table or View so that
> I can use that View or Table
> as Input to a Crystal Report. <<
>
> The term is adjacency list model. Can you explain "flatten it out" a
> bit more clearly. If you want to get the levels of tha tree, then that
> can be done in a VIEW with the nested sets model.
>
> The immediate superior is given by:
>
> SELECT B.emp AS boss, E.emp
> FROM OrgChart AS E
> LEFT OUTER JOIN
> OrgChart AS B
> ON B.lft
> = (SELECT MAX(lft)
> FROM OrgChart AS S
> WHERE E.lft > S.lft
> AND E.lft < S.rgt);
>
> or more generally, to find the level of each emp, so you can print the
> tree as an indented listing. Technically, you should declare a cursor
> to go with the ORDER BY clause.
>
> SELECT COUNT(O2.emp) AS indentation, O1.emp
> FROM OrgChart AS O1, OrgChart AS O2
> WHERE O1.lft BETWEEN O2.lft AND O2.rgt
> GROUP BY O1.lft, O1.emp
> ORDER BY O1.lft;
>
> Just add a parameter for the subtree root node and do some math.
>
> --CELKO--
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in your
> schema are. Sample data is also a good idea, along with clear
> specifications.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
- Next message: Tod: "RE: Access SQL to T-SQL"
- Previous message: JJ: "Re: Why am I getting this syntax error calling a SP?"
- In reply to: Joe Celko: "Re: Reflexive table problem - tricky"
- Next in thread: Joe Celko: "Re: Reflexive table problem - tricky"
- Reply: Joe Celko: "Re: Reflexive table problem - tricky"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|