Skip to content
Advertisement

Tag: sql-order-by

Recursive Subquerying with sorting

I looked at Tim Hall’s excellent article here, that allows you to work with self-referenced entities and show hierarchical data (starting with top level nodes and joining back recursively), using CTE like syntax in Oracle. So I have code that looks like this: For the the anchor rows (the top level hierarchy J1 entries in my SQL, with NULL parents),

Is order preserved after UNION in PostgreSQL?

Here is the code: As you can see the recursive code gives the result in right order, but the non-recursive code does not. They both use union, why the difference? Answer Basically, your query is incorrect to begin with. Use UNION ALL, not UNION or you would incorrectly remove duplicate entries. (There is nothing to say the trail cannot switch

Advertisement