I’m encountering some issues with hierarchical structure query in Oracle. Specifically, I’m trying to build a query to represent a hierarchical structure, but the results are not as expected. Here are the details: I created a table in my Oracle database as:
CREATE TABLE EMP ( DNO NUMBER, ENAME VARCHAR2(10), SAL NUMBER );
INSERT INTO EMP (DNO, ENAME, SAL) VALUES (101, 'John', 3000), (101, 'Michael', 2500), (101, 'Scott', 4000), (102, 'Smith', 1500), (102, 'Williams', 2000), (102, 'James', 3500), (103, 'Andrew', 1000), (103, 'Becker', 4500);
When I inserted the above data, my EMP table looked like this:
DNO | ENAME | SAL |
---|---|---|
101 | John | 3000 |
101 | Michael | 2500 |
101 | Scott | 4000 |
102 | Smith | 1500 |
102 | Williams | 2000 |
102 | James | 3500 |
103 | Andrew | 1000 |
103 | Becker | 4500 |
I want my output to be displayed in a hierarchical structure where the DNO is shown only once, followed by its ENAME and SAL. My expected output is as follows:
DNO | ENAME | SAL |
---|---|---|
101 | John | 3000 |
Michael | 2500 | |
Scott | 4000 | |
102 | Smith | 1500 |
Williams | 2000 | |
James | 3500 | |
103 | Andrew | 1000 |
Becker | 4500 |
I tried several ways, but they didn't work. Can someone help me? Thanks in advance.