Subject | Re: Loading a tree with CTE in preorder |
---|---|
Author | ainpoissee |
Post date | 2010-02-11T07:50:17Z |
Hi Svein,
ain
> You need a JOIN in the unioned query. Here's a quick suggestion that will not work, but it might have had D and AM been CHARACTER fields:Fortunately the nature of the D and AM is such that I can combine them into an NUMERIC(10,4) which is suitable for ordering.
> WITH RECURSIVEIt seems that unioned part (T/TT2) can't refer back to "top query" (TT1) but using "T.MyOrderBy" instead of "TT1.MyOrderBy" seems to work the way you intended... my initial quick test is promising :)
> Tree AS(
> SELECT TT1.UID, TT1.Parent, TT1.D, TT1.AM, TT1.D ||'.'|| TT1.AM AS MyOrderBy
> FROM TAB_Tree TT1
> WHERE TT1.D BETWEEN 100 AND 200
> AND TT1.Parent is null
> UNION ALL
> SELECT TT2.UID, TT2.Parent, TT2.D, TT2.AM, TT1.MyOrderBy ||'.'|| TT2.AM
> FROM Tree T
> JOIN TAB_Tree TT2 ON T.UID = TT2.Parent
> )
> SELECT UID, Parent, D, AM FROM Tree
> ORDER BY MyOrderBy
>
> Note that I rarely use Fb2 (and hence CTEs, in particular recursive CTEs), and would be surprised if this worked at all.
ain