Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sort a query based on a hierarchical table

Posted on 2008-10-15
3
Medium Priority
?
452 Views
Last Modified: 2013-12-19
I created the query below to generate a tree of nodes/groups. What it's output should look like what is below it.

But I can't seem to figure out a way to sort the information. If I add the order by at the end of the query it will create all the nodes first, then the node groups altogether. I showed a example of the output on the bottom. I can't even get the root level groups to alphabetize, I put them in the "nl.name in ('GMMARS')" in order, but they still come up in a seemingly random order
##
## Query
##
SELECT
	lpad(' ',4*(level-1))||case when nl.name=' ' then nn.node_name else 'Group:'||nl.name end
FROM
	opc_nodehier_layout nl LEFT JOIN opc_node_names nn ON nl.node_id=nn.node_id
START WITH
	nl.name in ('GMMARS','NewMMARS')   
CONNECT BY
	PRIOR nl.layout_id=nl.parent_id;
 
##
## Query Output Without Sort
##
NewMMARS
    Server 3
    Server 1
    Server 2
GMMARS
    GMMARS_MPUAT
        gmmarsat1
        gmmarsat2
    GMMARS_SPT
        gmmars2a
        gmmars3a
        gmmars4a
        gmmars5a
        gmmars6a
    GMMARS_MPSYS
        m1t
        gmmars2t
        gmmarsst1
        gmmarsst2
    GMMARS_Prod
 
##
## Query Output With Sort
##
    Server 3
    Server 1
    Server 2
        gmmarsat1
        gmmarsat2
        gmmars2a
        gmmars3a
        gmmars4a
        gmmars5a
        gmmars6a
        m1t
        gmmars2t
        gmmarsst1
        gmmarsst2
NewMMARS
GMMARS
    GMMARS_MPUAT
    GMMARS_SPT
    GMMARS_MPSYS
    GMMARS_Prod

Open in new window

0
Comment
Question by:Aludaan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 22727905
use the SIBLINGS keyword to sort hierarchical data
SELECT
        lpad(' ',4*(level-1))||case when nl.name=' ' then nn.node_name else 'Group:'||nl.name end
FROM
        opc_nodehier_layout nl LEFT JOIN opc_node_names nn ON nl.node_id=nn.node_id
START WITH
        nl.name in ('GMMARS','NewMMARS')   
CONNECT BY
        PRIOR nl.layout_id=nl.parent_id
ORDER SIBLINGS BY nl.name

Open in new window

0
 

Author Closing Comment

by:Aludaan
ID: 31506590
That was perfect sdstuber. You rock.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22737495
glad I could help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question