SQL recursion is not that common and many SQL dialects simply do not support it. FileMaker is one of them. Its SQL is fairly basic (e.g. it doesn't have LIMIT
) and usually less efficient than native FileMaker approach (e.g. if you use an independent subquery, it still seems to run anew for each row in the main query). That is if you work with FileMaker and are interested in recursive SQL, it's bound to be a purely academic exercise. If you're after it, then this Wikipedia article on hierarchical and recursive SQL might be a good start.
But you don't need SQL for what you're trying to do; you can do all this with rather simple FileMaker calculations. Assuming your hierarchical table uses ID
and Parent ID
and two relationships, Parent
and Child
, create the following fields:
Root ID =
If( IsEmpty( Parent::ID ); ID; /* else */ Parent::Root ID )
Leaf IDs =
If( IsEmpty( Child::ID ); ID; /* else */ List( Child::Leaf IDs ) )
Ancestor IDs =
List( Case( not IsEmpty( Parent::ID ); Parent::Ancestor IDs ); ID )
Descendant IDs =
List( ID; Case( not IsEmpty( Child::ID ); List( Child::Descendant IDs ) ) )
As you see each field has a recursive formula that refers to itself. You won't be able to enter it right after you add the field because by this time the field is not yet saved and FileMaker will complain that it cannot find it. To work around this first create a field, save it, and then edit it again and enter the formula.