Return the managers and Employees that reports to a manager

Hello Everyone,
I need your help in the following query using connect by:
I have 2 tabels
Tabel_1
Department hierarchy
department_id parent_department_id has_manager
1 NULL X
2 1
3 1 X
4 2 X
5 4
tabel_2
emp_id department_id is_manager
1 1 X
2 1
3 1
4 2
5 2
6 4 X
7 4
8 4
9 3 X
10 5 X
now in my company the concept of direct employees is as follow:
my direct employees are: the employees in my department + only the manager in the department below me in the hierarchy.
if the department bellow me has no manager then also these employees in that department reports to me
then we check the lower department and so on untill we find a manger then this manager reports to me but his employees reports to him so they are not my direct employees
for the above example should be as follows
if i want to get the direct employees of emp_id= 1
then result is
2
3
4
5
6
9
Best regards
Ibrahim

Hi, Ibrahim,
Welcome to the forum!
Whenever you have a question, please post CREATE TABLE and INSERT statements for your sample data. Since this is your first post, I'll do it for you:
CREATE TABLE     department
(       department_id          NUMBER     PRIMARY KEY
,     parent_department_id     NUMBER
,     has_manager          VARCHAR2 (1)
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (1,            NULL,               'X');
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (2,            1,               NULL);
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (3,            1,               'X');
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (4,            2,               'X');
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (5,            4,               NULL);
CREATE TABLE     emp
(       emp_id          NUMBER     PRIMARY KEY
,     department_id     NUMBER
,     is_manager     VARCHAR2 (1)
INSERT INTO emp (emp_id, department_id, is_manager)
             VALUES (1,       1,                 'X');
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (2,       1,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (3,       1,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (4,       2,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (5,       2,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
        VALUES (6,       4,                 'X');
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (7,       4,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (8,       4,                 NULL);
INSERT INTO emp (emp_id, department_id, is_manager)
         VALUES (9,       3,                 'X');
INSERT INTO emp (emp_id, department_id, is_manager)
        VALUES (10,      5,                 'X');
913209 wrote:if the department bellow me has no manager then also these employees in that department reports to me
then we check the lower department and so on untill we find a manger then this manager reports to me but his employees reports to him so they are not my direct employeesWhat if a department directly below that has no manager? For example, if we add this to the sample data:
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (6,            2,               NULL);
INSERT INTO department (department_id, parent_department_id, has_manager)
                     VALUES (7,            6,               NULL);Would anyone working in departments 6 or 7 (as well as 1 or 2) be included? I assume so. If that's wrong, then you don't need CONNECT BY: a self-join would be enough.
Here's one way to do that:
WITH     my_departments     AS
     SELECT     department_id
     ,     LEVEL          AS lvl
     ,     SYS_CONNECT_BY_PATH (has_manager, '/')     AS manager_path
     FROM     department
     START WITH     department_id     = (
                                     SELECT  department_id
                               FROM    emp
                               WHERE   emp_id       = 1     -- Parameter
                               AND     is_manager  = 'X'
     CONNECT BY     parent_department_id     = PRIOR department_id
SELECT       e.emp_id
FROM       my_departments     d
JOIN       emp               e  ON   d.department_id  = e.department_id
WHERE       (    REGEXP_LIKE ( d.manager_path
                        , '^/X/*$'
       AND  e.emp_id        != 1          -- Parameter
OR       (    REGEXP_LIKE ( d.manager_path
                       , '^/X/*X$'
       AND  e.is_manager     = 'X'
ORDER BY  e.emp_id
;Another thing you should do in every thread is say which version of Oracle you're using. The query above will work in Oracle 10.1 and higher.
The tricky part of thsi query is that we really want to use this CONNECT BY clause:
...     CONNECT BY     parent_department_id     = PRIOR department_id
          AND     has_manager          IS NULLbut to go one more level, that is, find not only the nodes above, but also their immediate children. I can't think of a good way to do that which is not more complicated (and probably less efficient) than what I posted above.

Similar Messages

  • I want Firefox to open to my homepage every time after it crashes. I do not want it to return to the tabs and applications that caused the crash. I am willing to lose information. How do I do this?

    Firefox crashes or seizes up when there are too many tabs and applications open, and causes my computer to crash or seize up. I use task manager to close Firefox or I restart my computer. Then, when Firefox restarts again, it automatically re-opens all the tabs and applications that caused the computer to seize or crash in first place. This is a waste of my time. I want Firefox to reopen to my homepage even if it crashes. I am willing to lose information to have Firefox reopen to a low-memory homepage. How do I do this?

    Set the Integer pref browser.sessionstore.max_resumed_crashes to 0 on the about:config page to get the about:sessionrestore page immediately with the first restart after a crash has occurred or the Task Manager was used to close Firefox.
    * http://kb.mozillazine.org/browser.sessionstore.max_resumed_crashes
    That will allow you to deselect the tab(s) that you do not want to reopen, but will allow to reopen other tabs.
    See:
    * http://kb.mozillazine.org/Session_Restore#Restoring_a_session_after_a_crash
    * http://kb.mozillazine.org/Browser.sessionstore.max_resumed_crashes
    See also:
    * https://wiki.mozilla.org/Session_Restore#Preferences
    To open the <i>about:config</i> page, type <b>about:config</b> in the location (address) bar and press the "<i>Enter</i>" key, just like you type the url of a website to open a website.<br />
    If you see a warning then you can confirm that you want to access that page.<br />
    *Use the Filter bar at to top of the about:config page to locate a preference more easily.
    *Preferences that have been modified show as bold (user set).
    *Preferences can be reset to the default via the right-click context menu if they are user set
    *Preferences can be changed via the right-click context menu: Modify (String or Integer) or Toggle (Boolean)

  • API call that will list the meetings of employees that are assigned to a manager

    Hi,
    What is the API call that will list the meetings of employees that are assigned to a manager?  The manager will be logged in and when I use the API call:
    http://[serverid]/api/xml?&action=report-my-meetings&principal-id=[##]
    I always get the same results which is a list of the meetings for the manager.  If put the id of the employee, I still get the managers meetings.
    I have looked through all of the API documentation and cannot find which call I could make to get the results that I want.  The documentation specifically says that report-my-meetings provides information about meetings that the logged in user is scheduled to attend.  So how do I get the information for the managers employees?
    What is needed is the Name, Duration, and Start Time of all meetings that the employee is enrolled in who is assigned to the manager that is logged in.
    Thanks for any help with this!

    I posted this in the wrong forum and have moved it to the XML forum.

  • Designing the consolidated and line item report for specific material

    Hi
    i have a R/3-report and it run only for 7-materials.That means we need to only those 7-materials in the selection screen of report.
    Then we will get each material corresponding line item details(material no, profit center, variable cost, quanity, price, financial loss(this is calculated based on quantity) , Utility Consumption(calculated based on price, variablecost)  ) in one report.
    Another report will give same for consolidated values of financial loss, utiliy consumption.
    <b>so how can i design the consolidated and lineitem details report for each material.</b> pls let me know
    kumar

    0VENDOR_ATTR - Vendor Master Data Attributes
    0FI_AP_51 - Check Register Line Items
    0FI_AP_4 - Accounts Payable Line Items
    It should be noted, however, that this won't get you everything that you need, so you may have to create enhance one or more of these DataSources and create User Exit ABAP code to get the required data. Also, the 0FI_AP_51 DataSource isn't available if your source system isn't ECC6 EHP3 or higher. If your source system is less than that, you're going to have to create a generic DataSource for the data from the PAYR table.

  • I want to clean all of the cookies and bugs that are in my computer causing it to be slow. What program should I use that is safe and won't mess up my computer?

    I want to clean all of the cookies and bugs that are in my computer causing it to be slow. What program should I use that is safe and won't mess up my computer?

    Problem description:
    I would like to clean up my computer. Its beginning to run slow and I believe cookies and bugs are apart of the reason.
    EtreCheck version: 2.0.6 (91)
    Report generated October 26, 2014 at 4:59:31 PM EDT
    Hardware Information: ℹ️
      MacBook Air (13-inch, Early 2014) (Verified)
      MacBook Air - model: MacBookAir6,2
      1 1.4 GHz Intel Core i5 CPU: 2-core
      4 GB RAM Not upgradeable
      BANK 0/DIMM0
      2 GB DDR3 1600 MHz ok
      BANK 1/DIMM0
      2 GB DDR3 1600 MHz ok
      Bluetooth: Good - Handoff/Airdrop2 supported
      Wireless:  en0: 802.11 a/b/g/n/ac
    Video Information: ℹ️
      Intel HD Graphics 5000 -
      Color LCD 1440 x 900
    System Software: ℹ️
      OS X 10.9.5 (13F34) - Uptime: 13 days 2:41:41
    Disk Information: ℹ️
      APPLE SSD SD0128F disk0 : (121.33 GB)
      S.M.A.R.T. Status: Verified
      EFI (disk0s1) <not mounted> : 210 MB
      Macintosh HD (disk0s2) /  [Startup]: 120.47 GB (70.38 GB free)
      Recovery HD (disk0s3) <not mounted>  [Recovery]: 650 MB
    USB Information: ℹ️
      Apple Internal Memory Card Reader
      Apple Inc. iPhone
      General USB Flash Disk 16.13 GB
      S.M.A.R.T. Status: Verified
      MSD (disk1s1) /Volumes/MSD : 16.13 GB (11.18 GB free)
      Apple Inc. BRCM20702 Hub
      Apple Inc. Bluetooth USB Host Controller
    Thunderbolt Information: ℹ️
      Apple Inc. thunderbolt_bus
    Gatekeeper: ℹ️
      Mac App Store and identified developers
    Kernel Extensions: ℹ️
      /Library/Extensions
      [loaded] foo.tap (1.0) Support
      [loaded] foo.tun (1.0) Support
    Startup Items: ℹ️
      tap: Path: /Library/StartupItems/tap
      tun: Path: /Library/StartupItems/tun
      Startup items are obsolete and will not work in future versions of OS X
    Problem System Launch Daemons: ℹ️
      [failed] com.apple.wdhelper.plist
    Launch Daemons: ℹ️
      [loaded] com.adobe.fpsaud.plist Support
      [running] com.perion.searchprotectd.plist Support
    User Launch Agents: ℹ️
      [loaded] com.genieo.completer.download.plist Support
      [loaded] com.genieo.completer.ltvbit.plist Support
      [running] com.genieo.completer.update.plist Support
      [loaded] com.google.keystone.agent.plist Support
      [invalid?] com.jdibackup.ZipCloud.autostart.plist Support
      [running] com.zeobit.MacKeeper.Helper.plist Support
    User Login Items: ℹ️
      iTunesHelper Application (/Applications/iTunes.app/Contents/MacOS/iTunesHelper.app)
    Internet Plug-ins: ℹ️
      DirectorShockwave: Version: 12.1.3r153 - SDK 10.6 Support
      Flash Player: Version: 15.0.0.189 - SDK 10.6 Support
      FlashPlayer-10.6: Version: 15.0.0.189 - SDK 10.6 Support
      JavaAppletPlugin: Version: 14.9.0 - SDK 10.7 Check version
      QuickTime Plugin: Version: 7.7.3
      Default Browser: Version: 537 - SDK 10.9
    User Internet Plug-ins: ℹ️
      TroviNPAPIPlugin: Version: 1.0 - SDK 10.9 Support
    Safari Extensions: ℹ️
      Trovi Search for Safari (Disabled)
    3rd Party Preference Panes: ℹ️
      Flash Player  Support
    Time Machine: ℹ️
      Time Machine not configured!
    Top Processes by CPU: ℹ️
          53% mds
          9% WindowServer
          3% mds_stores
          3% storeagent
          2% hidd
    Top Processes by Memory: ℹ️
      920 MB mds
      31 MB com.apple.WebKit.WebContent
      30 MB Messages
      29 MB Safari
      21 MB WindowServer
    Virtual Memory Information: ℹ️
      31 MB Free RAM
      713 MB Active RAM
      681 MB Inactive RAM
      1.09 GB Wired RAM
      17.85 GB Page-ins
      1.95 GB Page-outs

  • How to do a DB lookup in ICM and return the ANI and Rewards Number to CTI

        Hello,
    Does anyone know how to take the ANI and do a database look up to get the customer loyalty program number?  After the number is collected, I need to return the ANI and the loyalty number to CTI using variable 5.  Any feedback on how to accomplish this? 

    Ok if you have succesfully connected to the Database and call come out of the Right branch in your DBLookUp Node.
    You need to create a Set Variable, If you want to display the Loyalty Number in Peripheral Variable 1, Choose that variable and Click th formula Editor. In the formula Editor, choose the Object Type as Database select the database and choose the vairable as Loyalty Number.
    Note :You should have all the Variable in the Databse Explorer created under this Databse.
    I have atached a scrrenshot like how to se the Customer Type in Peripheral Variable 1
    By Default ANI should be there in the Agent Desktop or Toolkit
    Pam Scarpino wrote: Thank you.  This is very helpful.  We are stuck on the formula now.  We want to return the calling line id and the loyalty number to CTI.  Do you know how the formula should look?
    Regards,
    Senthil
    Rate if this helpful

  • Dynamically pass the username and password to report server to invoke reports

    Hi
    The scenario is as follows:-
    9iAS is installed under - Windows 2000 (web server and report Server 6i) and everything works fine.
    We have developed a page using pl/sql cartrige (oracle 8i), from that a report is invoked with some parameters. However, the userid and password is stored under keymap file (cgicmd.dat).
    Is there anyway to pass dynamically the username and password to report server in order to invoke the report.?
    Can any one advise?
    null

    Check out
    http://serverfault.com/questions/371907/can-you-pass-user-pass-for-http-basic-authentication-in-url-parameters
    Kind regards,
    Margriet Bruggeman
    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

  • How to read the method and put that code in 1 internal table

    Hi,
    Actually in the normal ABAP, if we want to read the report,  then we will write read report statement.
    If we want to read method then this statement will not work.
    So, to read the method and put that code in 1 internal table , which statement we need to write?
    Regards,
    Radhika

    Hi Naimesh,
    Thanks for your reply.
    As, i am not aware of ABAP Objects i am having some issues:
    Actually that is working for some methods in some classes.
    Here 1 issue is there:
    In se24 1 method is there in 1 class.
    Even by using that FM in SE37 i am able to get the program name by giving class and method name,
    but while using that FM in the program i am not able to get that program name.
    so, i am unable to read the source code of the program?
    1 example here:
    Class name is CL_GUI_FRONTEND_SERVICES.
    in this class, methods like FILE_EXIST , FILE_GET_SIZE, etc are there.
    By giving the class name and method name i am able to get the program name also,
    but while using that FM in the program i am not able to get that program name.
    so, i am unable to read the source code of the program?
    Here is my code:
    REPORT  Z16059_SCAN_METHOD.
    DATA: BEGIN OF I_PROGRAM OCCURS 0,
          LINE(256) TYPE C,
          END OF I_PROGRAM.
    DATA METHOD TYPE PROGRAM.
    DATA: BEGIN OF I_STRUCTURE,
          CLS_NAME(30) TYPE C,
          METH_NAME(61) TYPE C,
          END OF I_STRUCTURE.
    I_STRUCTURE-CLS_NAME = 'CL_GUI_FRONTEND_SERVICES'.
    I_STRUCTURE-METH_NAME = 'CL_GUI_FRONTEND_SERVICES-FILE_EXIST'.
    CALL FUNCTION 'SEO_METHOD_GET_INCLUDE_BY_NAME'
      EXPORTING
      MTDKEY             =  I_STRUCTURE
    IMPORTING
       PROGNAME          = METHOD
    EXCEPTIONS
       INTERNALMETHOD_NOT_EXISTING      = 1
       OTHERS                             = 2
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    READ REPORT METHOD INTO I_PROGRAM.
    Please help me out in this regard.
    Thanks & Regards,
    Radhika

  • Query for the FULL AND FINAL SETTLEMENT REPORT

    Hi,  any one  has the query for the FULL AND FINAL SETTLEMENT REPORT in oracle hrms??

    Hi,
    I go through that .class file and find that they have used below package in that.
    PAY_IN_TERM_RPRT_GEN_PKG
    Hope this will help you.

  • Run a report and have that report print to several printers - font

    Hello,
    In 10g, want to use the distribute=yes and destination parameters to run a
    report and have that report print to several printers. The destination file is
    an XML file that sends the output to printer. The distribution actually works
    but the output comes out as a very very small font. Its the correct data but
    one would need a magnifying glass to see.
    Regards,

    Any ideas?
    If i print at one printer, the font on the paper(report) good, if i printing at more printers at the same time, the font is really small!!
    Tnx,

  • Ok Apple users...Is there a way to delete e-mails on my Mac and not have to delete the same e-mails on my iPhone, and iPad...and vise-versa. I have the Cloud, and thought that everything would sync all of the time. If you know of a setting I need to adjus

    Ok Apple users...Is there a way to delete e-mails on my Mac and not have to delete the same e-mails on my iPhone, and iPad...and vise-versa. I have the Cloud, and thought that everything would sync all of the time. If you know of a setting I need to adjust, please let me know.

    Are you referring to @mac.com, @me.com or @icloud.com emails? Or some other email providers emails?
    If the former, it should do that automatically. If the latter, no. iCloud does not (nor is it supposed to) sync non-Apple provided emails.

  • I am trying to sync an old ipod nano (2nd generation) to my itunes on a windows pc but the itunes doesn't recognize the nano and says that it is synced with another computer. Unfortunately, I don't have the old computer now. How do I sync this nano to itu

    I am trying to sync an old ipod nano (2nd generation) to my itunes on a windows pc but the itunes doesn't recognize the nano and says that it is synced with another  computer. Unfortunately, I don't have the old computer now. How do I sync this nano to itunes ?

    See Recover your iTunes library from your iPod or iOS device.
    tt2

  • I have two Apple ID's.  How do I move all the songs and videos that I bought using Apple ID "A" over to Apple ID "B".  I plan to use Apple ID "B" from here on out.  Thanks

    I want to move all the songs and videos that I purchased using Apple ID "A" over to Apple ID "B". 
    I plan to use Apple ID "B" from here on out. 
    Thanks to all. 
    I'm using Macbook Air
    OS X ver 10.9.3

    You can't do this yourself; if desired, you can ask the iTunes Store staff if they'll do it, but they probably won't.
    (108496)

  • HT1382 how can I restore my back up files from my laptop? i want to see the dates and time that I backed up  my files. is it posssible?

    how can I restore my back up files from my laptop? i want to see the dates and time that I backed up  my files. is it posssible?

    What backed up files?
    Available backups for an iOS device can be found in Edit > Preferences > Devices.

  • Look at your iphoto 'page'. On the left is a margin running vertically down the page and within that margin are shown your files/folders - e.g 'events', I can't get that margin contents back

    Look at your iphoto 'page'.
    On the left is a margin running vertically down the page and within that margin are shown your files/folders - e.g 'events', 'last 12 months', 'anniversary' etc etc.
    Now visualize the 'page' but WITHOUT the margin and the entries in it. It just disappeared while I was editing photos.
    I've tried all the 'File', 'Edit', 'View', 'Share' 'Window' & 'Help' options, but no success.
    There is no apparent way of calling up onto the screen any photos, folders etc. because I can't get the margin and its contents back onto the iphoto page.
    I've tried restoring the page from my backup, where it is all perfect, but that didn't work.
    Help please!

    Move your cursor to the extreme left hand side of the iPhoto Window until it turns into a '+' and then click and drag right.

Maybe you are looking for

  • Getting the Server Details.

    Hi All, I am required to develop a Composite Application Service which will give the server details like HOSTNAME and PORT NO of the deployment server.Is there any way of fetching these details ? Could someone please help me out with this issue ? Reg

  • BAPI_REQUISITION_CHANGE error message

    Hi. I'm trying insert a Purchase Requisition Item - and the Accounts related - using the BAPI_REQUISITION_CHANGE. The problem is that I'm getting this error - translating the message from portuguese to english: "Item 00020 doesn't exist and can't be

  • Fax not working

    I just purchased a 8610 and I keep getting a variety of error messages 321, 358, and 388.  I used to have the 8600 and was able to fax without a problem until about two weeks ago.  I have tried all the solutions available on the site and nothing seem

  • Polling flag....?

    Hello Could Plz.. explain me what is a polling flag and how it works in process chain?(i bit confused with its definition) clear me regarding this. thanks and regards dj reddy.

  • Can't activate Messaging or FaceTime after iOS7 upgrade.

    Already tried to restore.