How to do this consult of a hierarchical recursion on a stored procedure? Help!

Please help me, im using oracle 11g

I have 2 tables

Department table (number of I_Depto (1), number of E_Depto (30));

i_depto e_dept

1 Finance

2 shopping

3 sistemas

4               rh

Empleado table (I_Emp number (5), N_Emp varchar (20), Q_salary (8.2) number, number of I_boss (5), the number of I_Depto (1));

i_emp n_emp q_salary i_boss i_dept

Juan 100 300 null 1

200 100 100 1 Pedro

300 Angélique 150 100 1

400 70 300 2 alberto

500 60 300 2 Miriam

600 3 500 80 moises

And yet, I have 3 conditions:

If the boss has 3 employees or more, he has increased his salary by 20%

If the boss have 2 employees, there was an increase in his salary of 10%

And if 0 or 1 employee, there is an increase of 5%

herarchical diagram is like that

-> Pedro Juan

Juan-> Angelica-> Alberto

Juan-Angelica-Miriam-Moses > > >

I have to present the name of the Department (e_depto) in the console and the result should be like this;



SQL > exec p_example ('finance');

Employees of BOSS of the boss pay increase employees (name) boss of the salary increase employee employees employees

juan             6                                              60                             pedro                                   0                                      5

juan             6                                              60                             angelica                               3                                    45

pedro           0                                                5                              NULL                                  0                                     0

Angelica       3                                              45                              alberto                                0                                     5

Anglica         3                                               45                              miriam                               1                                     3

SQL > exec p_example ('shopping');

Employees of BOSS of the boss pay increase employees (name) boss of the salary increase employee employees employees

alberto             0                                              3.5                             -                                   0                                      0

miriam             1                                                3                         moises                            0                                    4

SQL > exec p_example('sistemas);

Employees of BOSS of the boss pay increase employees (name) boss of the salary increase employee employees employees

moises             0                                            4                              -                                  0                                      0

His need to create a recurrence, stored procedure

The query I have is this, but it only shows me that the boss and the boss employees.

createor replace procedure deptos (depto varchar) as

t_boss varchar (49);

t_employ varchar (50);

cursor c_depto (t_depto varchar) is

Select a.n_emp boss, employee of b.n_emp

of empleado a, b empleado, departamento d

where a.i_emp = b.i_boss (+) and d.i_dept = a.i_dept

and a.i_emp in (select i_EMP from empleado where i_dept = t_dept);

Start

Open c_depto (depto);

loop

extract the c_depto in t_boss t_employ;

When the output c_depto % notfound;

dbms_output.put_line (t_boss |) Chr (9) | Chr (9) | t_employ);

end loop;

end;

/

Hello

I know it's a pain to post CREATE TABLE and INSERT statements for the sample data.  Think about why it is necessary: that people who want to help you can recreate the problem and test their ideas.  If you post statements that do not work, which does not recreate the problem or to test their ideas.  Test (and, if necessary, correct) your statements before committing.  Make sure all quotes are balanced.

I don't understand the desired output.  For example, when n_emp = 'Juan', why is-employees_ofthe_boss = 6, 5 no?  When n_emp is 'Angelic', why is-salary_increase = 45, not 30?  When n_emp = 'Alberto', why is-salary_increase = 5, not 3.5?

You may want something like this:

WITH universe_dup AS

(

SELECT i_emp, n_emp, q_sueldo, i_jefe

SYS_CONNECT_BY_PATH (i_emp, ","). ',' AS line

,       LEVEL                                    AS lvl

Of empleado

START WITH (IN) i_depto

SELECT i_depto

OF Department

WHERE e_depto = 'Finance' - or what you want

)

CONNECT BY i_jefe = PRIOR i_emp

)

universe

(

SELECT i_emp, n_emp, q_sueldo, i_jefe

MAX (lineage) DUNGEON (DENSE_RANK LAST ORDER BY lvl) AS lineage

MIN AS min_level (lvl)

Of universe_dup

GROUP of i_emp, n_emp, q_sueldo, i_jefe

)

got_salary_increase AS

(

SELECT a.i_emp, a.n_emp, a.i_jefe, a.min_level

EARL of (d.i_emp) AS employee_cnt

, a.q_sueldo * CASE

WHEN COUNT (d.i_emp) > = 3 PUIS.2

WHEN COUNT (d.i_emp) = 2 PUIS.1

OF AUTRE.05

END AS salary_increase

OF the universe a

LEFT OUTER JOIN universe d ON d.lineage LIKE '%', | a.i_emp | ',%'

AND d.i_emp <> a.i_emp

GROUP OF a.i_emp, a.n_emp, a.i_jefe, a.min_level, a.q_sueldo

)

SELECT p.n_emp AS boss

p.employee_cnt AS employees_ofthe_boss

p.salary_increase AS salary_increase_boss

c.n_emp AS employees_name

, NVL (c.employee_cnt, 0) AS employees_ofthe_employee

, NVL (c.salary_increase, 0) AS salary_increase_employee

OF got_salary_increase P

LEFT OUTER JOIN got_salary_increase c ON c.i_jefe = p.i_emp

WHERE p.min_level = 1

;

Depending on your data and your needs, it may be more effective to do "START WITH i_jefe IS NULL", as one of the above.  I guess "START WITH i_depto (...)" in the largest tables, when efficiency is more important.

Tags: Database

Similar Questions

  • How can I find a large amount of data from a stored procedure?

    How can I find a large amount of data to a stored procedure in an effective way?

    For example do not use a cursor to go through all the lines and then assign values to variables.

    Thanks in advance!

    >
    How can I find a large amount of data to a stored procedure in an effective way?

    For example do not use a cursor to go through all the lines and then assign values to variables.
    >
    Leave the query to create the object back to you.

    Declare a cursor in a package specification than the result set gives you desired. And to declare a TYPE in the package specification which returns a table composed of % rowtype to this cursor.

    Then use this type as the function's return type. Here is the code example that shows how easy it is.

    create or replace
        package pkg4
          as
            CURSOR emp_cur is (SELECT empno, ename, job, mgr, deptno FROM emp);
            type pkg_emp_table_type is table of emp_cur%rowtype;
            function get_emp(
                             p_deptno number
                            )
              return pkg_emp_table_type
              pipelined;
      end;
      / 
    
     create or replace
        package body pkg4
          as
            function get_emp(
                             p_deptno number
                            )
              return pkg_emp_table_type
              pipelined
              is
                v_emp_rec emp_cur%rowtype;
              begin
                  open emp_cur;
                  loop
                    fetch emp_cur into v_emp_rec;
                    exit when emp_cur%notfound;
                    pipe row(v_emp_rec);
                  end loop;
              end;
      end;
      / 
    
    select * from table(pkg4.get_emp(20));
    
         EMPNO ENAME      JOB              MGR     DEPTNO
    ---------- ---------- --------- ---------- ----------
          7369 DALLAS     CLERK2          7902         20
          7566 DALLAS     MANAGER         7839         20
          7788 DALLAS     ANALYST         7566         20
          7876 DALLAS     CLERK           7788         20
          7902 DALLAS     ANALYST         7566         20
    

    If you return a line an actual table (all columns of the table) so you don't need to create a cursor with the query a copy you can just declare the type like this % rowtype tables table.

     create or replace
        package pkg3
          as
            type emp_table_type
              is
                table of emp%rowtype;
            function get_emp(
                             p_deptno number
                            )
              return emp_table_type
              pipelined;
      end;
      / 
    
     create or replace
        package body pkg3
          as
            function get_emp(
                             p_deptno number
                            )
              return emp_table_type
              pipelined
              is
              begin
                  for v_rec in (select * from emp where deptno = p_deptno) loop
                    pipe row(v_rec);
                  end loop;
              end;
      end;
      / 
    
  • Windows service install could not be consulted how to fix this?

    Windows service install could not be consulted how to fix this?

    Hi raptor05

    What exactly happens when you try to install a program that needs this service?

    Try these steps and see if it helps you:

    Unregister and re-register Windows Installer.

    To do this, follow these steps:
    1. click on start and then click Run.
    2. in the Open box, type msiexec /unreg and then press ENTER.
    3. click on start and then click Run.
    4. in the Open box, type msiexec/regserver, and then press ENTER.

    NOTE: you must log in using the administrator account and try to install it like that. Or you can also try running the program by right clicking and select "run as Administrator". It should work.

    We need to install the latest version of Windows Installer that is 4.5, the link to it is:

    http://www.Microsoft.com/downloads/details.aspx?FamilyId=5A58B56F-60B6-4412-95B9-54D056D6F9F4&displaylang=en

    After back and let us know if it helped to solve your problem.

    Thank you and best regards,

    R uma - Microsoft technical support.

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • iPhone 5 s to connect with blue tooth in the car for hands-free calling does not connect to the speaker, it works very well with the iPhone 5. How to solve this problem?

    IPhone 5 IOS 9.2 (13 c 75) I can log in my car with Blue Tooth to make phone calls, but it does not connect to the speaker. To establish the connection with the car, the procedure has been completed correctly. With the iPhone 5 it works perfectly correct.

    How to solve this problem? Is there someone who has the same problem?

    Howdy,

    Thank you for using communities Support from Apple.

    I see you try to get your iPhone to interface with the stereo in your car via Bluetooth.  For setting this up, please follow the steps below.

    If you use Bluetooth

    1. Consult the user manual of your car stereo to get the procedure to a Bluetooth device.
    2. On your iOS device, drag up to open Control Center, then press ontwice to turn on Bluetooth and turn it back on.
    3. Restart your iOS device.
    4. On your iOS device, Cancel the twinning of your car radio. On the screen of your car désapparier your iOS device and any other device. Restart your car and your iOS device, then pair and connect again.
    5. Update your iOS device.
    6. Install the updates to the firmware of your car radio.
    7. If you still not connect, contact Apple technical support.

    Help to connect your iPhone, iPad or iPod touch with your car radio

    See you soon.

  • on my key board when I try to type a letter that has a number or a symble on it it type the number or the symble how to fix this?

    on my key board when I try to type a letter that has a number or a symble on it it type the number or the symble how to fix this?

    Make a lot of assumptions here...

    Keyboards only I know who have numbers/symbols on the letter keys would be a laptop or netbook without a numeric keypad.  Assuming that this is the case, the most likely cause is that you have the 'digital Locks' active.  To return to a regular keyboard, you must disable the NumLocks.  On a laptop, how to do varies from a Builder (in other words, if that doesn't work, download or consult the manual of your computer)... but usually this is done by pressing the "FN" key at the bottom of the keyboard while pressing the 'Num Lock' key, which is usually on the box at the top right of the keyboard.

    HTH,
    JW

  • I can not connect on DPS App Builder. This error occurs: ERROR this account does not have permission to access the App generator. Learn more about how to fix this.

    I can not connect on DPS App Builder. This error occurs: ERROR this account does not have permission to access the App generator. Learn more about how to fix this.

    Then you need to either:

    (1) sign up for cloud creative then you can build a simple edition approx. attention however: this approach will no longer valid as of May 1, when we withdraw support for a unique creative cloud and unique edition apps are only on devices iPad.

    (2) sign up for a full license of DPS. You can request information on prices using Adobe | Request for consultation.

    Neil

  • 6.4 how to set up the product category hierarchies

    Hello!

    I would like to ask for help in the understanding of this document:

    Oracle® Fusion Middleware Reference Guide for Oracle Business Intelligence Applications 11 g Release 1 (11.1.1).

    There is a Section "how to Set Up product category hierarchies" 6.4:

    To customize the product category hierarchies:

    Using the tool of Administration Oracle BI, open OracleBIAnalyticsApps.rpd.

    Create a link with the PROD_CATn_WID of the W_PRODUCT_D table column.

    But I do not understand how to do this because he has all the details on how to do that.

    Help, please!

    In fact, I need to install a product hierarchy in BI applications. We have this hierarchy:

    Brand/group/subgroup.

    There is a real problem while understanding how to set up this hierarchy in OBIEE.

    Thanks for any help and information.

    The document you are looking at is not for 7.9.6.4 - it is for the BI Applications against merger applications.

    The Bug you see seems to have been fixed before the 7.9.6.4 output.

    At the cash register an object in the designer, make sure that the adapter is first opened.  Right-click on the folder and open... the file will be "BOLD".  Then go back to the mapplet and departure (right click, Versioning, Check-out).

    Please give the points.

  • Since iOS recent upgrade to my 5s my battery drains too quickly.  How to fix this problem?

    I recently downloaded version 10.0.2 Apple software update on my 5 and the battery drains too quickly.  It never happened until this update.  How to fix this problem?

    Hello kellyfromrexford,

    I understand that you are having a singing of fast draining of the battery you upgraded to iOS 10 on your iPhone 5s.  To determine what application or process affect the life of your battery, take a look at the information in these resources:

    On the use of the battery on your iPhone, iPad and iPod touch

    Maximize the life of the battery and battery life

    Take care.

  • Highlighted in yellow, a section of a message cannot find how to do this.

    I received messages where the highlights of person in yellow a phrase or section of my message they respond to-please, how to do this?

    Messages on the Mac does not support rich text (e.g. highlighting, pointed out, etc) creation (even with copy/paste styles). Someone using Thunderbird, or MS Outlook client e-mail you can send messages with yellow text highlighting, because Apple Mail includes in HTML background colors. Apple Mail has no native user controls to apply a (lasting) nail on the compound, or responded by e-mail.

    You can compose your response by email in TextEdit, apply the highlight yellow, crossed and other features, you can then export of Rich Text in an HTML document. You double-click on it, and it will open in Safari. You then copy/paste the content of Safari read in the body of your Apple Mail compose window. Most will not want to do that, because it's just annoying.

    Successfully, I sent and received the original e-mail, including the (invisible) HTML markup, including the yellow background, in Apple Mail. Your mileage may vary depending on the features of the agent from the user for the mail to the recipient.

  • My mac does not detect my hard drive after the upgrade. How to solve this problem?

    My mac does not detect my hard drive after upgrading to Mac OS Sierra. How to solve this problem?

    Please provide more detailed information. You can read write an effective communities of Apple support question. Maybe you could open disk utility and take a screen snap so that we can see what you see.

    To post a screenshot

    1. Press command + shift + 4, that will change the cursor to the line of sight.
    2. Hold down the mouse button and the collimator allows you to select the part of the screen you want to capture.
    3. Release the mouse button and the image will be saved on your desktop.
    4. Click on the camera icon in the toolbar of the message forum Editor.
    5. Drag the image on the button select a file and click the Insert button.
  • I downloaded Safari 10 and now every page that uses Java asks me to activate or not. How to avoid this new feature? I have problems to access my bank account.

    I downloaded Safari 10 and now every page that uses Java asks me to activate or not. How to avoid this new feature? I have problems to access my bank account.

    I think they mean JavaScript.

    In your Safari menu bar, click Safari > Preferences and then select the Security tab, and then select: Enable JavaScript

  • How cancel a monthly fee for storage space I need is no longer? Does anyone know how to do this?

    How cancel a monthly fee for storage space I need is no longer? Does anyone know how to do this?

    You can change the storage volume by selecting a new plan directly from your iPhone, iPad, iPod, Mac or PC (for example, a plan of 5 GB to replace a 200 GB plan). The downgrade will take effect after the end of your monthly or annual subscription in progress.

    iOS


    1. If you use the iOS 8 or laters, go to settings > iCloud > storage. If you are using an older version of iOS, go to settings > iCloud > storage and backup.
    2. Click Change storage Plan.
    3. Press Downgrade Options and enter your Apple ID.
    4. Choose a different plan to use when the expiration of your current storage plan.
    5. Tap done.


    OS X


    1. Choose the Apple menu > System Preferences, and then click iCloud.
    2. Click on manage in the lower corner on the right.
    3. In the upper right corner, click Change storage Plan.
    4. Click Options to Downgrade.
    5. Enter your Apple ID password, then click on manage.
    6. Choose a different plan to use when the expiration of your current storage plan.
    7. Click done.
  • iBooks author - TOC shows the empty entries for table - how to stop this?

    Hello

    I spent hours trying to find an answer to this.

    My book has a few tables - nothing wrong with that.  I have a front cap of the table - that's what I want in the table of contents.

    iBooks author shows instead, empty entries whenever he comes across a table, that is hopeless.

    I just want the topic as indicated above - 2.1.  I don't want 2.2 which is the table.

    Does anyone have an idea how to do this, please?

    If this isn't the case, iBooks author is desperate for any book that contains the tables.

    See you soon,.

    Mike

    Hello

    I found an article from Apple which may help to answer your question and I pasted the link below. I think that you will have to 'say' iBA this is or is not part of the Table of contents (TOC) and's done it with the Inspector tool, Document tab, section TOC. Look down at the bottom of this window to the sign and arrow down to allow the addition of other document settings in the table of contents, or remove some.

    I hope that this help =)

    iBooks author: Customize the table of contents

  • I have Quicktime 7, but when I try to open iTunes I get a message saying that I need to update Quicktime 7.5.5. How to do this?

    I have Quicktime 7, but when I try to open iTunes I get a message saying that I need to update Quicktime 7.5.5. How to do this?

    Click here to install or run the software update from the system preferences panel.

    (143939)

  • Can't see taskbar, except if I close all the windows, but hide automatically is not checked - how to fix this?

    It happened all of a sudden during a browsing session. Also, I do not see the albums "open windows" list unless I have point to him. "Auto-hide taskbar" is not checked. I tried to stop and do a restart, but nothing has changed. The two bars are always top in IE but not in Firefox more. It is very annoying. I would like to know how to solve this problem, and also how it could have happened, so I don't do it again! Thanks in advance.

    Firefox is stuck in mode full screen? The F11 function key switches between regular views and full screen modes.

    Firefox window is an irrational dimension in normal mode, it can indicate a corruption of the file that stores the information of location/size of window. The usual medicine is to rename this file and let Firefox restart by using the default dimensions. Here's how:

    Open the settings folder (AKA Firefox profile) current Firefox help

    • button "3-bar" menu > "?" button > troubleshooting information
    • (menu bar) Help > troubleshooting information
    • type or paste everything: in the address bar and press Enter

    In the first table of the page, click on the view file"" button. This should launch a new window that lists the various files and folders in Windows Explorer.

    Leave this window open, switch back to Firefox and output, either:

    • "3-bar" menu button > button "power".
    • (menu bar) File > Exit

    Pause while Firefox finishing its cleanup, then rename xulstore.json to something like xulstore.old. If you see a file named localstore.rdf, rename this to localstore.old.

    Launch Firefox back up again. Windows normally appear again?

    As this process goes probably most toolbars, you can use one of the following methods to display the toolbar list and activate the desired bars it:

    To activate the menu bar, toolbar bookmarks or other bars, click it in the list.

Maybe you are looking for

  • error console does not open

    Somewhere along the line, I lost the ability to open my error Console and my guests to download. I tried to delete my profile and the dumping of the application, reinstall, but it still does not work. I tried to install the beta version of Firefox 6

  • HP Officejet Pro K550: Software to clean the print head

    I need tool box for old HP printer: HP Officejet Pro K550, for 10-64 bit Windows.I need a software tool to clean the print head!

  • Tx1000 and quick launch buttons

    HelloI have a Tx1000 (REF RX695AV) on which I did a clean install of Windows 7 ultimate x 64.The touchscreen works can I open/close and drag the programs with the stylus.  So do the buttons (Play, following precedent and Stop) multimedia. What does w

  • What version of Windows I have?

    What version of Windows I have? I can not also read the sticker because it is worn out and the laptop will not completely start or repair, recovery disks are lost. Part number is readable bbut paert looks like: Can I use any dissk installation of re

  • My office can work with two operating system - Windows vista and Windows XP Profession (both real)

    I currently have Windows Vista 32-bit preinstalled by DELL, when I bought my computer Dell E-520 in July 2007. Before that, I had a computer of IBM Office Professional XP was preloaded. This IBM desktop computer was transferred to my daughter and she