Hierarchy in a single column, with a hole more

Hello
I have the following query, which built a hierarchy:

with it_dept (orga3) as
(
Select '0305' across double Union
Select "03050202" of any double union
Select "03050105" of any double union
Select "03050001" of any double union
Select "03050106" of any double union
Select "03050203" of any double union
Select "03050107" of any double union
Select "0305000101" of any double union
Select "0305000102" of any double union
Select "0305000103" of any double union
Select "0305020201" of any double union
Select "0305020202" of any double union
Select "0305020203" of any double union
Select "0305020301" of any double union
Select "0305020302" of any double union
Select "03050204" of any double union
Select "0305020401" of any double union
Select "0305020402" of any double union
Select "0305010501" of any double union
Select "0305010502" of any double union
Select "0305010601" of any double union
Select "0305010602" of any double union
Select "0305010701" of any double union
Select "0305010702" of any double union
Select "030501" of any double union
Select '030502' double
)
Select parent_org,
child_org,
level lvl
Of
(
Select parent_org null,
child_tbl.orga3 child_org
of it_dept child_tbl
where length (child_tbl.orga3) = (select min (length (orga3))
of it_dept
)
the Union / * cover the hole * /.
Select '0305' parent_org,
'030500' child_org
of the double
Union
Select parent_tbl.orga3 parent_org,
child_tbl.orga3 child_org
of it_dept parent_tbl
outer join left it_dept child_tbl
on parent_tbl.orga3 = substr (child_tbl.orga3, 1, length (parent_tbl.orga3))
and (child_tbl.orga3) length = length + 2 (parent_tbl.orga3)
)
Connect prior child_org = parent_org
Start with parent_org set to null.

1. the data is organized in such a way that they build a hierarchy. Each level of
This has the same number of digits.
2. the data of the hierarchy is on a specific data column (i.e.: orga3).
3. There's a hole in the hierarchy, which means that a member is missing (namely: the 2nd member of '030500' level).

Issues related to the:
(1) is there a simpler way to achieve the same result?
(2) is it possible do not write the missing member, in order to cover the hole?
(3) is it possible do not write + 2 (as in the line "and length = length + 2 (parent_tbl.orga3) (child_tbl.orga3)") for
in terms of getting the members of next level?


Note: I'm using OracleDb 11 g v.2

Thank you
SIM

When I see a problem that really needs a better model of data, I wonder: ' how would I migrate data to a better model?

Once I wrote the SQL code to migrate the data, the SQL to meet the requirement is simple.

If you cannot migrate the code, just run the two SQL bits, one after the other.

For example, the test data:

create table it_dept(orga3) nologging as
select '0305'      from dual union all
select '03050202'  from dual union all
select '03050105'  from dual union all
select '03050001'  from dual union all
select '03050106'  from dual union all
select '03050203'  from dual union all
select '03050107'  from dual union all
select '0305000101' from dual union all
select '0305000102' from dual union all
select '0305000103' from dual union all
select '0305020201' from dual union all
select '0305020202' from dual union all
select '0305020203' from dual union all
select '0305020301' from dual union all
select '0305020302' from dual union all
select '03050204'  from dual union all
select '0305020401' from dual union all
select '0305020402' from dual union all
select '0305010501' from dual union all
select '0305010502' from dual union all
select '0305010601' from dual union all
select '0305010602' from dual union all
select '0305010701' from dual union all
select '0305010702' from dual union all
select '030501'    from dual union all
select '030502'    from dual;

To migrate the data, I have to assume that there is difference of 2 bytes between child and parent. If you introduce a difference of 1 or 3 bytes, we're all lost.

create or replace view new_it_dept as
with minlen as (
  select min(length(orga3)) minlen from it_dept
)
, filled_hierarchy as (
  select distinct substr(orga3,1,minlen+(level-1)*2) orga3,
  minlen
  from it_dept, minlen
  connect by orga3 = prior orga3
  and prior sys_guid() is not null
  and length(orga3) >= minlen+(level-1)*2
)
select
case when length(orga3) > minlen
  then substr(orga3,1,length(orga3)-2)
end dad,
orga3 son
from filled_hierarchy;

If you create a table with that point of view, you have migrated data. If you cannot migrate, then simply use the view in your query.

select dad, son, level from new_it_dept
start with dad is null
connect by dad = prior son;
DAD SON LEVEL
0305 1
0305 030500 2
030500 03050001 3
03050001 0305000101 4
03050001 0305000102 4
03050001 0305000103 4
0305 030501 2
030501 03050105 3
03050105 0305010501 4
03050105 0305010502 4
030501 03050106 3
03050106 0305010601 4
03050106 0305010602 4
030501 03050107 3
03050107 0305010701 4
03050107 0305010702 4
0305 030502 2
030502 03050202 3
03050202 0305020201 4
03050202 0305020202 4
03050202 0305020203 4
030502 03050203 3
03050203 0305020301 4
03050203 0305020302 4
030502 03050204 3
03050204 0305020401 4
03050204 0305020402 4

Tags: Database

Similar Questions

  • For all the records for each record double, I need to get a single column with null or 0.

    Hi all

    I have a requirement where I need to get all the records, for each record in double, I need to get a single column with null or 0.

    create table a1

    (

    Identification number,

    VARCHAR2 (100), the point

    part varchar2 (100));

    Insert into a1

    values (1, 'ABC', 'A1');

    Insert into a1

    values (2, 'DEF', 'A2');

    TABLE A

    PART ITEM ID

    1 ABC A1

    1 ABC A1

    1 ABC A1

    DEF 2 A2

    DEF 2 A2

    3 DEF A2

    O/P

    PART ITEM ID

    1 ABC A1

    1        ABC             0

    1        ABC             0

    DEF 2 A2

    2       DEF              0

    3       DEF              0

    Thanks in advance.

    Thanks for your help FrankKalush...

    This one will work.

    WITH got_r_num AS

    (

    SELECT NVL (a1.id, a1.id) as id

    NVL (a1.item, a1.item) AS element

    NVL (a1.part, a1.part) IN the framework

    a1.id AS a_id

    ROW_NUMBER () OVER (PARTITION BY a1.id

    ORDER BY NULL

    ) AS r_num

    BY the a1

    )

    SELECT id

    element

    CASE

    WHEN a_id IS NOT NULL

    AND r_num = 1

    THEN part

    ELSE ' 0'

    END in the framework

    OF got_r_num

    ;

  • SQL - Multiple Fetch in a single column with a comma separator

    Hello Experts,
    Good day to all...

    I need your help on the following scenarios. The following query returns all channels titleID. Rather than print them one under the other as a result of the query, I want the output to be in the batch of 25 values.i.e than each line must have 25 values separated by commas. IE if there are 100 titles satisfying the output, then there should be only four lines with and each line with 25 titles in comma separated way.
    SELECT DISTINCT title_id
               FROM pack_relation
              WHERE package_id IN (      SELECT DISTINCT fa.package_id
                                                    FROM annotation fa
                                                GROUP BY fa.package_id
                                                  HAVING COUNT
                                                            (fa.package_id) <100);
    I tried with the PL/SQL block; Whereas it is printing all the values permanently :(
    I have to stop with 25 values and display.

    If its possible with SQL block alone. then it would be a great help

                                                           
                                                                          
    DECLARE
       v_str   VARCHAR2 (32767)  := NULL;
    
       CURSOR c1
       IS
         SELECT DISTINCT title_id
               FROM pack_relation
              WHERE package_id IN (      SELECT DISTINCT fa.package_id
                                                    FROM annotation fa
                                                GROUP BY fa.package_id
                                                  HAVING COUNT
                                                            (fa.package_id) <100);
    BEGIN
       FOR i IN c1
       LOOP
          v_str := v_str || ',' || i.title_id;
       END LOOP;
       v_str := SUBSTR (v_str, 2);
       DBMS_OUTPUT.put_line (v_str);
    EXCEPTION
       WHEN OTHERS
       THEN
          DBMS_OUTPUT.put_line ('Error-->' || SQLERRM);
    END;
    Thank you...

    You can use CEIL

    Code example

    SELECT
        nt,
        LTRIM(MAX(SYS_CONNECT_BY_PATH(val,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concat_val
    FROM
        (
            SELECT
                val,
                nt,
                ROW_NUMBER() OVER (PARTITION BY nt ORDER BY val)    AS curr,
                ROW_NUMBER() OVER (PARTITION BY nt ORDER BY val) -1 AS prev
            FROM
                (
                    SELECT
                        level                          AS val,
                        ceil(rownum/3)  as nt /* Grouped in batches of 3 */
                    FROM
                        dual
                        CONNECT BY level <= 10
                )
        )
    GROUP BY
        nt
        CONNECT BY prev = PRIOR curr
    AND nt              = PRIOR nt
        START WITH curr = 1;
    
            NT CONCAT_VAL
    ---------- --------------------------------------------------------------------------------
             1 1,2,3
             2 4,5,6
             3 7,8,9
             4 10
    

    Your code

    SELECT
        nt,
        LTRIM(MAX(SYS_CONNECT_BY_PATH(title_id,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concat_val
    FROM
        (
            SELECT
                title_id,
                nt,
                ROW_NUMBER () OVER (PARTITion BY nt ORDER BY title_id)   AS curr,
                ROW_NUMBER() OVER (PARTITION BY nt ORDER BY title_id) -1 AS prev
            FROM
                (
                    SELECT
                        title_id,
                        ceil(rownum/25) AS nt /* Grouped in batches of 25 */
                    FROM
                        pack_relation tdpr
                    JOIN annotation fa
                    ON
                        tdpr.package_id = fa.package_id
                    GROUP BY
                        title_id,
                        fa.package_id
                    HAVING
                        COUNT (fa.package_id) < 500
                )
        )
    GROUP BY
        nt
        CONNECT BY prev = PRIOR curr
    AND nt              = PRIOR nt
        START WITH curr = 1;
    
  • two rows of back with a single column with just one place

    Hello all;
    I have a query that returns two rows similar to this below
    
    ID      PLACE            PROGRAM
    A       NEWYORK      PROGRAM A
    A       NEWYORK      PROGRAM B
    
    I would like this instead
    
    ID      PLACE             PROGRAM
    A       NEWYORK       PROGRAM A
                                   PROGRAM  B
    
    All help is appreciated. Thank you.
    Published by: user13328581 on March 22, 2011 11:52

    user13328581 wrote:
    WOW... Thanks a lot for Salomon, I never used the partitions and row_number in such a way... can you please explain your logic.

    Ensure that:

    ROW_NUMBER() over (partition by order of identification by location, program)

    This operation can take all the rows returned by the query andsplit them in compartments (sheet music) by id. Inside each bucket it order lines by program and place and assign line numbers. So for each row of the ID number 1 will be stored with (in alphabetical order) first place the first programt. And this is the line where we want to ID is displayed. That's why we wrap the analytical function that precedes in the CASE statement that will do just that. Now:

    ROW_NUMBER() over (partition by id, order by program)

    pretty much the same just bucket is a combination ID / PLACE. So for each combination of it, we want to show that for the first (in alphabetical order) programt.

    Now notice in the ORDER BY clause I prefix ID and PLACE with the table alias. You do it because otherwise the query alias ID and PLACE will have priority resulting in the incorrect sort order (remember that we cancelled all ID and PLACE with the exception of the number of line 1).

    SY.

  • Trying to create a Section to multiple columns with Sections of the single column before and after

    Is there a way to create a section break that is not default to a new page after I created columns on a page.  I created the columns and under them, I want to return to the normal formatting for the rest of the single page.  See picture attached.

    Any rejection of Pages v5 has a break of presentation which was present in the Pages ' 09 v4.3. This will allow you to transition to several columns and back to single column on the same page. In the v5 Pages, you can insert 3 text boxes and change the 3-column layout in the Middle text box. You use the toolbar item Insert to inject column breaks when you want to start a new list in the next column. I'll show this screenshot below.

    You can fake your layout in Pages v5 using 3 text boxes and setting 3 columns in the Central text area. Better to show the mode of provision for this and in a text box, the column outlines are not displayed. After each column list, you then choose column break the Insert point toolbar menu to move to the next column, add a list, repeat. Each column will expand downwards. Click on the following to enlarge.

    Pages ' 09 v4.3 using layout breaks Pages using 3 text boxes V5.6.1                                                      

      

  • How to fill a column with numbers and maintain when adding or deleting lines?

    So far, I've discovered two ways to fill a column with the number:

    1. Enter '1' in a single cell, '2' in the one below, select both, and then use the yellow dot to drag down - it will do the rest.

    2 create a formula such as 'A2 + 1' and drag also.

    However, in both cases it will work if I manually this continues to do for each newly added line at the bottom. Now, that alone would not be that big of a problem if it wasn't for the fact that I'm working with a table where the lines can be added among other rows in the future.

    Having said that, could someone please tell me how to create a column with a number that will increase automatically? I mean - if I add a new line between line 56 and 57, I want the new line to have a '57"in it, and the" old 57 "would now be"58"and so on." " I don't want to drag the whole column, which can be hundreds if not more than a thousand lines.

    Try to put this in the cells in the column:

    = ROW()

    In the formula, you can add or subtract a number if necessary to get the starting number that you need.

    SG

  • Remove the clicked point of Listbox (single column)

    Hello

    It seems a lot of posts on clear lines of the programmatically multicolumn listbox but not the only column listboxes.

    Woth the help of Martins and GerdW, helped me build a subset of a ListBox with items clicked in a reference list

    Make a table of items clicked in a list

    . How it adds a feature to delete just in case rather than the deletion of the entire list and do it all over again.

    Thanks in advance.

    Have an array of strings to the "REF" enter in the list box and store it on a shift register. When you remove an item (for example, for an event), remove this item from the list (using the removal of the table) and write back to the property Ref of the listbox. A single-column list box works exactly the same way as a multicolumn listbox.

    (Excuse the broken links to properties - what happens when you create an excerpt)

  • Two foreign keys of a table in a single column

    Hi gurus

    I wonder that can we add two foreign keys of a table on a single column, I think the answer is, but what should be the reason behind this?

    Appreciate if someone there explain to me...

    Concerning

    Muzz

    Hello Muzz,

    Perhaps the example of human Chen reveals a design error.
    If a student has a 'teacher_id' "teacher" FK and FK for "emp" becaue each teacher is an EMP, I guess that the correct design would be to have only the FK of 'teacher' and to have another pointing to the 'emp' table in the 'teacher' tabe FK

    But we can imagine comical situations...
    CREATE TABLE PEOPLE (id NUMBER of KEY PRIMARY, name VARCHAR2 (20 CHAR),...);
    CREATE TABLE SCIENTIST (id people KEY PRIMARY NUMBER (id) REFERENCES, discipline VARCHAR2 (20 CHAR),...);
    CREATE TABLE POLICEMAN (id REFERENCES to KEY PRIMARY NUMBER (id), hire_date people DATE,...);

    First table = all.
    2nd: a subset of the first, with people having the profile of 'scientific '...
    3rd: a subset of the first, with people being a policeman.
    It might be people first table and in none of the 2nd and 3rd, 1st and 2nd, 1st and 3rd only or in the 3 tables.

    Then we can have a fourth table of 'something' referring to a person who must be a scientist and a police officer.

    Here is a sqlplus session illustrating; the last piece: I try inserting several lines in xxxx, only the last is accepted.

    SQL > CREATE TABLE PEOPLE (id NUMBER of the PRIMARY KEY, name VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE SCIENTIST (people of REFERENCES of KEY PRIMARY NUMBER id (id), discipline VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE POLICEMAN (people of REFERENCES of KEY PRIMARY NUMBER id (id), DATE hire_date);
    Table created.

    SQL > CREATE TABLE XXXX (id PRIMARY KEY NUMBER, people_id NUMBER, any other VARCHAR2 (30 CHAR));
    Table created.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxsci FOREIGN KEY (people_id) scientific xxxx REFERENCES (id);
    Modified table.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxpol FOREIGN KEY (people_id) police xxxx REFERENCES (id);
    Modified table.

    SQL > INSERT INTO person VALUES (100, 'John');
    1 line of creation.

    SQL > INSERT INTO person VALUES (120, 'Mary');
    1 line of creation.

    SQL > INSERT INTO person VALUES (103, 'Tom');
    1 line of creation.

    SQL > INSERT INTO person VALUES (123, "Bruno");
    1 line of creation.

    SQL > INSERT INTO VALUES of scientific (120, 'Chemistry');
    1 line of creation.

    SQL > INSERT INTO scientific VALUES (123, 'Mathematics');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (103, DATE ' 2001-04-01');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (123, DATE ' 1998-07-01');
    1 line of creation.

    SQL > INSERT INTO xxxx VALUES (1, 456, "nothing");
    ERROR on line 1:
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 100, "only to people");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 120, "only the learned");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 103, "only police officer");
    ORA-02291: integrity constraint (SYS. FK_XXSCI) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 123, 'ok');
    1 line of creation.

    Best regards

    Bruno Vroman.

  • Cannot create a clean column with links

    Dear friends of the APEX,

    I'm developing an interactive report and you want to have in it a clean column with links instead of the standard "pencil" - column.

    I explain it by the example of https://apex.oracle.com/pls/apex/f?p=81683:1:

    I want that "Deptno" column had links with the viewer just as single line "pencil" - column a.

    My idea was to change the type of column "Deptno" of plain text to link.

    But, unfortunately, when I did in the pictures Page and save the modified Page Builder type column to return to 'plain text '.

    Anyone here know, what I did wrong?

    Thank you very much!

    LHOST

    Franck wrote:

    I'm developing an interactive report and you want to have in it a clean column with links instead of the standard "pencil" - column.

    I explain it by the example of https://apex.oracle.com/pls/apex/f?p=81683:1:

    I want that "Deptno" column had links with the viewer just as single line "pencil" - column a.

    Why would you do?

    My idea was to change the type of column "Deptno" of plain text to Link.

    If unique lines are not identified using a PK column, add ROWID to the projection of the report query and set the Type of this column to hidden column.

    Set the Type of the DEPTNO column on link, the link target #, #DEPTNO #and attributes of link to link text class = 'a-IRR-detail-row' data-line-id = "" #ROWID # "." Replace "ROWID" with the PK column name when unique lines are identified with a single column value. Save the changes.

    But, unfortunately, when I did in the pictures Page and save the modified Page Builder type column to return to 'plain text '.

    Anyone here know, what I did wrong?

    You changed the type of column 'Link' but did not enter the other properties needed to operate it as a link.

  • Selection of Pixel single column, then paste...

    Hello

    This might look like a very unusual request, but I was upset to find the answer.

    In a given photographic image, I would choose a single pixel in each row of pixels, running up and down in a columnof a pixel.  Then, I want to eliminate (Cup) nothing else on the Web with the exception of the one column of selected pixels. From there, I would take the pixel selected in each line and copy the pixel color on each line on the left and right of the pixel selected.

    The end result is that I have a series of horizontal lines of pixels height of one color (based on the color of the selected pixel) through the image of left and right.

    Looking at this picture:

    Select a single pixel on each line in a single column-

    xxxxxx XXXXXXS

    xxxxxx XXXXXXS

    xxxxxx XXXXXXS

    Remove/cut everything except the selected pixels in only one column-

    ...... S......

    ...... S......

    ...... S......

    Now, on each line, copy single pixel selected to the left and the right of the selection, to the extent of the canvas-

    SCOTTS SCOTT

    SCOTTS SCOTT

    SCOTTS SCOTT

    Interesting challenge or Easy-Peasey?

    Thank you!

    Easy. Use the brand single pixel tool to select a line of pixels. Cmd/Ctrl-J to copy pixels into a new layer. CTRL/cmd-click on the layer icon to select these pixels. CTRL/cmd-T to turn those pixels on the width of the image.

  • Single-column-multi-Rows, columns by using Types of Collection

    Can you guys help me get the result after using types of collection or all the predefined collections?

    In fact, I'm trying to compare the results of the columns col1 and col2 against a single value, so try to get the output in a single column.

    Required output should be like

    SQL > with t as (1 select col1, col2 2 double)

    2 Select * from t;

    COL

    ----------

    1

    2

    Please suggest any solutions using regexp/connect by / or etc.

    Well, then:

    SQL> with t as (select 1 col1, 2 col2 from dual)
      2  select nt.column_value
      3  from t
      4     , table(sys.odcinumberlist(col1, col2)) nt
      5  ;
    
    COLUMN_VALUE
    ------------
               1
               2
    

    but it is overkill IMO.

  • Several links in the single column

    Hello

    I have a requirement to display several links in the single column.

    Example:

    Select emp_name emp #, total_days days, col3, col4 from table

    Here, I need to display a link beside the example of days

    If day > 100 then display "101 CH" where 101 days value and CH is the URL link which will open a popup page. I can't have another column for the link that we have already several columns. So I need a way to display the link with the value next to him. How can I achieve this?

    try to put it in the source of the region

    Select emp_name, emp #,.

    -case when days > 100 then days | " http://Google.com" target = "_blank" > popup' another to_char (days) end total_days, "

    col3, col4 from table;

    This should display "101 popup" when the days more than 100, no popup link on days<= 100="" e,g="" "99"="">

    and the value of the column total_days to the "Reporting Standard column" to allow html tags.

    Change the href attribute and formatting to meet your requirement

  • Excellent loading from data / last copy takes all the data from column to single column

    I use oracle apex apex.oraclecorp.com, I set up a new page to load data for one of the table Simple (with 4 column of numeric values and varchar 1) Ex: component (VARCHAR), Q1 and Q2 & Q3 and Q4 are (NUMBER) based in columns

    In the CSV, I gave the data like this when I move the CSV file on the page "under data source to load', click Next

    COMPONENT1ST QUARTER1ST QUARTERQ3Q4
    ABCD100200300400
    DCBA90807060

    I see all the data IS SHOWN IN THE single COLUMN: PFA screenshot

    I also tried with packaged Application: "loading samples" there in the same way

    Please let me know if I m missing something

    Thank you

    CHS

    Hello

    Looks like you need to specify the right column delimiter (comma, tab, etc.). Check which delimiter is used in your csv and make sure that you specify in the field "separator".

    Kind regards

    Vincent

    http://vincentdeelen.blogspot.com

  • My page is displayed in Design view, in a single column?

    In Design view, my page is displayed in a single column, all the boxes of the AP are displayed on the other. In Live View mode, the page appears correctly. Also, if I find this page with a browser, yet once, it appears correctly, what's wrong with my mode of creation? Help :-)

    (1) the design mode is often a poor representation of what your page will look like

    (2) PA divs are not how to design modern web pages

    (3) If you want more help, you must show us your site, or at least to include the code - we can only guess.

  • Merge two column in single column value

    Team,

    I have a requirement where in I need to merge 2 column values in a single column.

    e.g. I Percent_complete column in my table. and my table has only one record.

    I need to ask questions that I need to store the values of percent_complete and (100-percent_complete) in a single column.

    ex

    Percentage complete is set to 30.

    I need to select the 30 and 70 (100-30) both in a single column.

    Thank you

    If you mean like this?

    SQL > ed
    A written file afiedt.buf

    1 with t (select 30 as double percent_complete)
    2  --
    3 end of test data
    4  --
    5. Select decode(rownum,1,percent_complete,100-percent_complete) as percent_complete
    6 t
    7 * connect by rownum<=>
    SQL > /.

    PERCENT_COMPLETE
    ----------------
    30
    70

Maybe you are looking for