Solved

levels for reporting

Posted on 2016-09-26
5
79 Views
Last Modified: 2016-11-21
Hi,
Below query gives below results:

select

*

from

(

Select

a.position_nbr,

a.REPORTS_TO,

a.POSITION_desc,

a.Position_level

ROW_NUMBER() OVER (PARTITION BY a.position_nbr ORDER BY a.EFFDT DESC) AS RN

from PS_POSITION_DATA a
)

where RN='1'


 

position_nbr reports_to   POSITION_desc Position_level

143               12                          CEO                                  CEO

038              143                        DIRECTOR                       DIR

0418             143                       ADVISOR                        ADV

114              143                       DG                                   DG

346              114                     Manager                          MAN

202              114                    Lead                                  LEAD

 

 

 
i want results like below so idea is start from level1 where position_level ='CEO ' and show who reports to CEO and then who reports to DG etc. Max level is 8

 

level1  level2 level3 level4 level5 level6 level7 level8  position_desc position_level

143                                                                                              CEO            CEO

          038                                                                                  DIRECTOR        DIR

          0418                                                                              ADVISOR         ADV

          114                                                                              DG              DG

                   346                                                                  Manager         MAN

                   202                                                                  Lead            LEAD
0
Comment
Question by:sam2929
5 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41817063
Here is the complete code

;WITH CTE
AS 
(

	Select position_nbr , reports_to , POSITION_desc , Position_level, 0 Level
	From PS_POSITION_DATA  E Where E.REPORTS_TO = 'CEO'
    
	UNION ALL
    
	Select E.position_nbr, E.reports_to, c.Level + 1 ,  E.POSITION_desc , E.Position_level
	From PS_POSITION_DATA  E INNER JOIN CTE c on c.position_nbr = e.REPORTS_TO 

)
SELECT * FROM CTE H 
	 

Open in new window


Enjoy !!
0
 
LVL 22

Expert Comment

by:Helena Marková
ID: 41817277
Mybe you can try hierarchical query:
SELECT LPAD(' ',2*(LEVEL-1)) || to_char(position_nbr) lvl ,
        POSITION_desc, Position_level
    FROM PS_POSITION_DATA
    START WITH Position_level = 'CEO'
    CONNECT BY PRIOR position_nbr = reports_to NOCYCLE;

http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_10002.htm#SQLRF01702
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 41817683
you can use sys_connect_by_path to display the hierarchy

select  position_nbr, REPORTS_TO, POSITION_desc, 
  level position_level, 
  sys_connect_by_path(position_desc, ' / ') reporting_chain
from PS_POSITION_DATA 
connect by prior position_nbr = reports_to 
start with POSITION_desc = 'CEO'

Open in new window

0
 

Author Comment

by:sam2929
ID: 41837728
pawan your code don't work
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41857957
@Author - Can you post the error you r facing ?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

830 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