Archive for November 23rd, 2009
In the previous article on recursive CTE‘s in SQL Server I demonstrated that they are not really set-based.
SQL Server implements the recursive CTE‘s syntax, but forbids all operations that do not distribute over
UNION ALL, and each recursive step sees only a single record from the previous step.
Now, let’s check the same operations in PostgreSQL 8.4.
To do this, we well write a query that selects only the very first branch of a tree: that is, each item would be the first child of its parent. To do this, we should select the item that would be the first child of the root, the select the first child of that item etc.
This is a set-based operation.
CONNECT BY syntax, despite being set-based, offers some limited set-based capabilities: you can use
ORDER SIBLINGS BY clause to define the order in which the siblings are returned. However, this would require some additional work to efficiently return only the first branch.
In a true set-based system, this is much more simple.
Let’s create a sample table:
Read the rest of this entry »