Found an interesting problem here.
ResultID ParentID ValueX
--------------------------
1 0 GrandParent
2 1 Parent1
3 1 Parent2
4 2 Child1
5 2 Child2
6 3 Child3
7 3 Child4
I want my query result set to look like this:
ResultID ParentID ValueX
--------------------------
1 0 GrandParent
2 1 Parent1
4 2 Child1
5 2 Child2
3 1 Parent2
6 3 Child3
7 3 Child4
If the ParentID is 0, it means that it's a major category. If the ParentID is greater than 0, it means it's a minor category, a child of the parent.
So the parents need to be ordered A-Z and the children need to be ordered A-Z as a group and within this group the values must be considered for sorting alphabetically. This is not limited to 3 levels and can go until 10.
Can you help me get this ordered correctly?
WITH resultset (resultid, parentid, valuex) AS (
SELECT 1,0,'Grandparent' FROM dual UNION ALL
SELECT 2,1,'Parent1' FROM dual UNION ALL
SELECT 3,1,'Parent2' FROM dual UNION ALL
SELECT 4,2,'Child1' FROM dual UNION ALL
SELECT 5,2,'Child2' FROM dual UNION ALL
SELECT 6,3,'Child3' FROM dual UNION ALL
SELECT 7,3,'Child4' FROM dual )
SELECT ResultID , ParentID, ValueX
FROM resultset
ORDER BY ????