How to write this query in the hierarchy

Hi gurus,

Really need your help on this query.  Thank you very much in advance.

SELECT
  t1.key as root_key ,
(SELECT
      t2.unit_id AS unit_id 
      level-1 AS level ,
      t2.name,
      t2.creator
    FROM
      tab t2
      START WITH t2.unit_id       =   t1.unit_id            -----check each node as root
      CONNECT BY prior t2.unit_id = t2.parent_unit_id

  )
   t1.name as parent_unit_name
FROM
  tab t1

I'll write a query of the hierarchy as above, and that EACH line (node, totally more than 10200) is checked as root node to see how many sheets are accessible for her... It must be implemented in a single query.

I know inline query should NOT return multiple rows or multiple columns, but the inline elements are necessary and can certainly be made in a correct solution.

(env):

Database Oracle 12 c Enterprise Edition Release 12.1.0.2.0 - 64 bit Production

PL/SQL Release 12.1.0.2.0

)

Test data:

select 1 as unit_id, null as parent_organization_unit_id, 'U1' as name from dual
union all
select 2, 1, 'U2' FROM DUAL
UNION ALL
SELECT 3, NULL, 'U3' FROM DUAL
UNION ALL
SELECT 4, 3, 'U4' FROM DUAL
UNION ALL
SELECT 5, 2, 'U5' FROM DUAL
UNION ALL
SELECT 6, 5, 'U6' FROM DUAL
UNION ALL
SELECT 7, 6, 'U7' FROM DUAL
UNION ALL
SELECT 8, 5, 'U8' FROM DUAL
UNION ALL
SELECT 9, 5, 'U9' FROM DUAL;

Final result should be like this

key unit_id,    level,   name, parent_name
1    1    0    u1      u1
1    2    1    u2       u1
1    5    2     u5      u1
1    6    3     u6      u1
1    7    4    u7       u1
1    8    3    u8       u1
1    9    3     u9      u1
2    2    0     u2       u2
2    5    1      u5       u2
2    6    2     u6       u2
2    7    3      u7      u2
2    8    2      u8       u2
2    9    2      u9       u2

Don't know how get you your output, it does not match your data...

with tab as)

Select 1 as unit_id, null as parent_organization_unit_id 'U1' as the name of double

Union of all the

Select 2, 1, 'U2' FROM DUAL

UNION ALL

SELECT 3, NULL, 'U3' FROM DUAL

UNION ALL

SELECT 4, 3, 'U4' FROM DUAL

UNION ALL

SELECT 5, 2, 'U5' OF THE DOUBLE

UNION ALL

SELECT 6, 5, 'U6' OF THE DOUBLE

UNION ALL

SELECT 7, 6, "U7" OF THE DOUBLE

UNION ALL

SELECT 8, 5, 'U8' FROM DUAL

UNION ALL

9. SELECT, 5, 'U9' FROM DUAL

)

Select dense_rank() key (order by connect_by_root unit_id), unit_id, level - 1 as 'LEVEL', connect_by_root name root_parent_name

t tab

Start with parent_organization_unit_id is null

Connect prior unit_id = parent_organization_unit_id

KEY UNIT_ID LEVEL ROOT_PARENT_NAME
1 1 0 "U1".
1 2 1 "U1".
1 5 2 "U1".
1 6 3 "U1".
1 7 4 "U1".
1 8 3 "U1".
1 9 3 "U1".
2 3 0 "U3".
2 4 1 "U3".

Tags: Database

Similar Questions

  • No idea how to write this query

    Hi, My Data is as below

    DocNum doc_date type of amount
    1154 15 November 11 232501.5 invoice
    200206 4 November 11 - 243672.64 credit memo
    Note flow 111 5 November 555.22 11

    Output must be

    DocNum doc_date amount Type AmountDR AmountCR
    1154 232501.5 15 November 11 Bill 232501.5
    Note credit 200206 4 November 11 - 243672.64 - 243672.64
    Note flow 111 5 November 555.22 11 555.22

    If the amount is > 0, then it must be displayed in the value of the amount to be AmountDR
    If amount < 0 then it must be displayed in the value of the sum amount CR



    Can help how to write this query
    with sample_table as (
                          select 1154 Docnum,date '2011-11-15' doc_date,232501.5 Amount,'Invoice' type from dual union all
                          select 200206,date '2011-11-04',-243672.64,'Credit Memo' from dual union all
                          select 111,date '2011-11-05',555.22,'Debit Memo' from dual
                         )
    select  Docnum,
            doc_date,
            Amount,
            type,
            case
              when Amount >= 0 then Amount
            end AmountDR,
            case
              when Amount < 0 then Amount
            end AmountCR
      from  sample_table
    /
    
        DOCNUM DOC_DATE      AMOUNT TYPE          AMOUNTDR   AMOUNTCR
    ---------- --------- ---------- ----------- ---------- ----------
          1154 15-NOV-11   232501.5 Invoice       232501.5
        200206 04-NOV-11 -243672.64 Credit Memo            -243672.64
           111 05-NOV-11     555.22 Debit Memo      555.22
    
    SQL> 
    

    SY.

  • How to write a query for the data exchange between two columns?

    How to write a query for the data exchange between two columns?

    I tried a request, does NOT work.
    update tmp t1 set t1.m1=t1.m2 and t1.m2=(select t2.m1 from tmp t2 where t2.student_id = t1.student_id)
    Thank you.

    Published by: user533361 on October 23, 2009 14:04

    Just plain and simple:

    update tmp t1
     set t1.m1=t1.m2,
         t1.m2=t1.m1
    /
    

    SY.

  • How to write this query?

    Hi people,

    I need to get a query in which a set of records, I get ONLY those which previous registry has a field with a value to this topic. Other values, the field can contain are not necessary.

    I know that sounds easy but... I can't get it.

    So, for Oracle 10 g 2... Here's my query:

    SELECT a.person_id, a.person_status, a.message_id, a.order_id

    OF t_HR one

    WHERE a.person_status = "rejected".

    AND a.id >

    (SELECT max (b.id)

    OF t_HR b

    WHERE b.person_id = a.person_id

    and b.order_id = a.order_id

    AND b.person_status! "revised =".

    B.ID AND < a.id)

    ORDER BY desc a.id

    Let me explain:

    1 - HR table is a table of people. These people has serveral STATUS.

    2 - ID is a sequential (each www.voyages-sncf.com has a different identification number).

    3 - the application must get THAT all people "rejected".

    4. - However, (subquery) I need ONLY those that previous register (the second register) holds a status of "OK". If the person holds a "revised" status he's not, he should be the next register (the third)

    5.-L' ORDER ID DESC, so is the first register must have a STATUS = "rejected" and the second a 'OK '.  IF the second register = "revised", then the third register must be 'OK '. And I need this query.

    HOW DO?

    My problem: the subquery gives you previous register of the same guy, but... it does not give you the value of the State, I need, which is 'OK '.

    I tried to add to the subquery...

    SELECT max (b.id)

    OF mod_human_resource b

    WHERE b.person_id = a.person_id

    and b.order_id = a.order_id

    AND b.person_status = 'OK '.

    AND b.id < a.id

    ... but if I have 5 records of that person, the first is "rejected", the second is "accepted", the third is 'new' and the fourth is 'OK'... the subquery gives you the 4th register and which is not correct for me, it must be only the second one (prior to the first State registry).

    I need to be a query, because I need to use it on a MERGER for a DWH.

    If there is another way (function, or even a procedure) to make the MERGER rather than with a request, which would be ok too. I am poor DWH knowledge.

    Thanks in advance.

    Hello

    So, you need to know if a line is the 'first' line, and you should also know what is the 'next' status, (even the 'first' and 'next' are already defined).  This sounds like a job for analytical functions.  ROW_NUMBER can tell you if a line is first or not, and LEAD can tell you what a value on the next row.

    Since you post CREATE TABLE and INSERT statements for your own table, I'll use the table scott.emp to illustrate.

    Consider these data from scott.emp:

    SELECT DeptNo

    ename

    work

    FROM scott.emp

    ORDER BY deptno

    ename DESC

    ;

    Output:

    DEPTNO ENAME JOB

    ---------- ---------- ---------

    10 MILLER CLERK

    PRESIDENT OF KING 10

    MANAGER 10 CLARK

    20 SMITH CLERK

    ANALYST SCOTT 20

    20 JONES MANAGER

    20 FORD ANALYST

    20 ADAMS CLERK

    30 WARD SALESMAN

    SELLER OF 30 TURNER

    30 MARTIN SALESMAN

    30 JAMES CLERK

    MANAGER BLAKE 30

    30 ALLEN SALESMAN

    Now, let's say we want only who know the departments where the forefront (in order descending ename) a job = 'CLERK', and the following line (also in descending by ename order) = "ANALYST" job, and we want to know the ename of the first row.  In other words, the correct output is:

    DEPTNO ENAME

    ---------- ----------

    20 SMITH

    Note that deptno = 10 is not included, even if the first task is to "CLERK." that was because the second job in deptno = 10 is the "PRESIDENT", not "ANALYST."

    Here's a way to get these results:

    WITH got_analytics AS

    (

    SELECT ename, deptno, job

    ROW_NUMBER () OVER (PARTITION BY deptno

    ORDER BY ename DESC

    ) AS r_num

    LEAD (employment) OVER (PARTITION BY deptno

    ORDER BY ename DESC

    ) AS next_job

    FROM scott.emp

    )

    SELECT deptno, ename

    OF got_analytics

    WHERE r_num = 1

    AND job = 'CLERK '.

    AND next_job = 'ANALYST '.

    ;

    I hope that answers your question.

    If this isn't the case, then, as Dan (and the FAQ forum) said, post CREATE TABLE and INSERT statements for some sample data and the exact results you want from these data.

    Post your query, based on the one I have posted more top and ponit out where he gets results.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

  • How to write a query for the given scenario?

    Hi all

    I have two tables EMP, DEPT with data below.
    EMP TABLE: -.
    SAL DEPID EMPLOYMENT ENAME EID
    111 RAM 1500 10 MANAGER
    222 SAM ASST MANAGER 2000 20
    KALA 333 2500 10 REGISTRAR
    444. BIMA 20 3000 MANAGER
    CHALA 555 MANAGER 3500 30
    RANI 666 ASST MANAGER 4000 10
    777 KAMAL MANAGER 2400 10

    DEPT TABLE: -.

    DEPID DNAME
    XX 10
    AA 20
    30 ZZ

    Q1: I want the sum of the salary of each Department and for the particular job. Here, in each Department manager, Assistant Manager, clerk positions are there.
    I want to display the result as below...
    10-20-30 JOBS
    --------------------------------------------------------------------------
    MANAGER OF 3900 3000 3500
    ASST MANAGER 4000 2000 NULL
    THE CLERK 2500 NULL NULL

    Please tell me how to write a sql query?

    Thank you
    SAI
    SQL> SELECT job,
      2         MAX(DECODE(deptno, 10, sum_sal)) "10",
      3         MAX(DECODE(deptno, 20, sum_sal)) "20",
      4         MAX(DECODE(deptno, 30, sum_sal)) "30"
      5  FROM (SELECT deptno, job, SUM(sal) sum_sal FROM emp GROUP BY deptno, job)
      6   GROUP BY job;
    
    JOB               10         20         30
    --------- ---------- ---------- ----------
    CLERK           1300       1900        950
    SALESMAN                              5600
    PRESIDENT       5000
    MANAGER         2450       2975       2850
    ANALYST                    6000
    
    SQL> 
    
  • How to write this query procedure of ina

    IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
    ) THEN
    SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
    WHERE V_BUSINESS_PASSCODE AND V_CUSTOMER_ID = & ID = & CODE AND
    V_LOG IN('Y')
    SO I HAVE TO RETURN THE ISP (OF TYPE VARCHAR) CAN YOU TELL HOW DO

    Make values flag, id, code as a parameter and an output parameter. You can have any number f settings if you want to return the results of the query.

    as
    create procedure (identification number, varchar2, varcahr2, out returnvalue flag code varchar2)
    as
    Start
    -your code

    ---

    --
    IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
    ) THEN
    SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
    WHERE V_CUSTOMER_ID = ID AND CODE = V_BUSINESS_PASSCODE AND
    V_LOG IN('Y');
    returnValue =;

  • How to write a query for the following scenario

    Hi I have a table as follows which have values of column as below
    column1     column2     column3     column4     column5     column6     column7     column8
    10000     10     1     7     15     10     9     0
    10001     11     2     7     19     0     8     1
    10002     12     3     9     8     7     15     0
    I want to find the values 7 and 15 on each line together in one of the columns of 4 to 8.

    Here, in case I get a first and third ranks.

    (Column1, Column2, Column3) is the primary key for the table

    Help, please

    Hena
    with data as
    (
    select 10000 as column1,     10 as column2,     1 as column3,     7 as column4,     15 as column5,     10 as column6,     9 as column7,     0 as column8 from dual union all
    select 10001,     11,     2,     7,     19,     0,     8,     1 from dual union all
    select 10002,     12,     3,     9,     8,     7,     15,     0 from dual
    )
    select * from data
    where 7 in (column4, column5, column6, column7, column8)
    and   15 in (column4, column5, column6, column7, column8)
    /
    COLUMN1                COLUMN2                COLUMN3                COLUMN4                COLUMN5                COLUMN6                COLUMN7                COLUMN8
    ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
    10000                  10                     1                      7                      15                     10                     9                      0
    10002                  12                     3                      9                      8                      7                      15                     0                      
    

    Edited because requirement changed while displaying the solution

    Published by: Cyn on October 30, 2009 15:27

  • How to write a query to return rows with the varchar column that contains even a single occurrence of the characters, such as Ÿ and

    How to write a query to return rows with the varchar column that contains even a single occurrence of the characters, such as Ÿ and

    I have a table whose columns with values such as

    MINNEAŸPOLIS and ¿VV ¿A

    Only the characters that are allowed in this column are alphabets, numbers, spaces, points and supports.

    Please help to write a SQL SELECT with Regexp_like query or any other option.

    Thanks to you all! Under query worked for me. Thank you Frank to explain the concept of hooks inside regexp_like.

    SELECT * FROM testspecial, WHERE REGEXP_LIKE (sampletext, "[^] ^ A - Z ^ a - z ^ 0-9 ^ [^.]") ^ {^} ^]') ;

  • How to find this value in the oracle database to find out the table

    Hi expert,

    I know there is a value in the oracle database, please show me how to find this value in the oracle database to find out the table that contains this value.

    Thank you very much

    918440 wrote:
    Hi expert,

    I know there is a value in the oracle database, please show me how to find this value in the oracle database to find out the table that contains this value.

    Thank you very much

    Go to the online documentation and discover the data dictionary. You may be interested in reading about ALL_TAB_COLUMNS, but you will need to know what you want until you can query the view to find information

  • How ot do this query

    some of you that it won't be easy, but for a rookie plsql is not form. Can U help?

    want to find out who has max (salary) and record that information in a variable

    Table: used
    id  salary
    1    2000
    2    2500
    3    1800
    Select max (salary) in employee == > back 2500
    But how to write this in a plsql and find the id is 3 and save it in a variable


    my incomplete statement:

    Select max (salary), in myvar to employee where id =?

    can you help me?

    Oh and save it in a variable plsql:

    declare my_var emp.empno%TYPE;
    begin
      select e1.empno into my_var
      from emp e1
      where e1.sal = (select max(e2.sal) from emp e2)
        and rownum = 1;
    end;
    /
    
  • I try to get Firefox to open Vuze when I click on a magnet link, I get the following message: Firefox doesn't know how to open this address, because the Protocol (loving) is not associated with any program. tell me the solution

    When I try to open a torrent using magnet, I get the following message. Firefox doesn't know how to open this address because the Protocol (loving) is not binding to any program.

    I had this problem and just reinstalled Firefox. Problem solved. In my case, I believe that it is through the installation of FlashGet. Although I uninstalled FlashGet, it had outstripped the Magnet links / Protocol. I hope this helps you with your instance. Fortunately, I had Setup for my version of Firefox (7.0.1) registered and used that but you might need to search for your current version or install a later version

  • I have updated to ios 9.3.1 now I have no sound for text messages alerts and alert sounds no calendar! I just missed a meeting disaster. Any ideas how to solve this problem, all the settings are correct.

    I have updated to ios 9.3.1 now I have no sound for text messages alerts and alert sounds no calendar! I just missed a meeting disaster. Any ideas how to solve this problem, all the settings are correct.

    Try a reset...

    Force restart your iPhone: tap on and hold the Home and Sleep/Wake buttons for at least ten seconds, until you see the Apple logo.

    If this does not help, tap settings > general > reset > reset all settings

  • Problem of the intranet: "Firefox doesn't know how to open this address, because the Protocol (k) is not associated with any program."

    Creation of an intranet to a collection of security videos and am linking to a video through a picture icon in the form of:
    < a href = "K: \'link to the movie" > ""
    < /a >

    Overview on the works of IE9, the movie starts when the splash screen is clicked, but in Firefox, I get the alert "Firefox doesn't know how to open this address, because the Protocol (k) is not associated with any program."

    InterLan VLC is the video player by default on the PC. I don't want to put video controls anywhere in the page.

    I code for XHTML 1.1 strict with CSS 2.1, and Firefox 5 handles all of my eccentric styles while IE9 has a few issues, while Firefox is my browser of choice.

    Looking at my Options in Firefox, 'File Windows Media audio/video' is configured to use VLC.exe, so I'm always looking to solve this problem.

    Any Suggestions?

    You must use the Protocol file:// (file:///k:/link_to_movie).
    Easier to see the links to use are to open a file via file > open a file

  • I have updated and now when I try to log on to Web sites with fingerprint reader, I get "Firefox doesn't know how to open this address, because the Protocol (dpql) is not associated with any program."

    I have updated and now when I try to log on to Web sites with fingerprint reader, I get "Firefox doesn't know how to open this address, because the Protocol (dpql) is not associated with any program."

    What is the Digital Persona fingerprint scanner? If it's that they have abandoned, for more details, see https://support.mozilla.com/en-US/questions/799388

  • Impossible to download music - error message reads "Firefox doesn't know how to open this address, because the Protocol (wmms) is not associated with any program."

    I bought a song to download from Wal - Mart.com and received the error message when I tried to download the song. "Firefox doesn't know how to open this address, because the Protocol (wmms) is not associated with any program." I've never had a problem before. I paid for this download & even I can't access it.

    The site of Wal-Mart offer a plugin or program (Walmart MP3 music downloads) to manage this Protocol?

    See:

    On a protocol must be enrolled in a program for Windows that supports this Protocol.

    See also http://kb.mozillazine.org/Register_protocol

Maybe you are looking for