You can do it this way. It first gets parents and assigns a rank to each one of them. Then it gets children and assigns parent rank again plus a child rank. At the end it gets the result from the union sorted by parent order and then child order.
You can add additional columns in ORDER BY
part of DENSE_RANK()
to further sort children.
;with cte as
(
select distinct parenttext, DENSE_RANK() OVER (ORDER BY parenttext) parentorder, 0 childorder
from @parchi
union all
select childtext, DENSE_RANK() OVER (ORDER BY parenttext), DENSE_RANK() OVER (ORDER BY childtext)
from @parchi
)
select parenttext from cte
order by parentorder, childorder