Numbering of the lines based on two columns

Hello world

I´d would like to know if there is a way to achieve the numbering shown in the second table below indicating only Oracle native functions, like ROW_COUNT() on the partition, etc.

I m using Oracle 10 g.

The logic used is:
From 1, increment one each time that the ORIGIN is identical to the FIRST ORIGIN of the line (ID) group.
ID    ORIGIN    DESTINATION    ORDER
------------------------------------
1     A         B              1
1     B         A              2
1     A         B              3
1     B         C              4
1     C         A              5
 
ID     ORIGIN    DESTINATION    ORDER    NUMBERING
--------------------------------------------------
1      A         B              1        1
1      B         A              2        1
1      A         B              3        2
1      B         C              4        2
1      C         A              5        2
In order to compare the ORIGIN of each line with the FIRST ORIGIN of the group, I used the function LAG to create a column that will be the FIRST ORIGIN of the value in the group.

However, I was not able to number the lines as shown above (column NUMBERING).

Any help will be much appreciated.

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

Test query:
WITH T AS
(
  SELECT 1 ID, 'A' ORIGIN, 'B' DESTINATION, 1 ORDERING FROM DUAL UNION ALL
  SELECT 1 ID, 'B' ORIGIN, 'A' DESTINATION, 2 ORDERING FROM DUAL UNION ALL
  SELECT 1 ID, 'A' ORIGIN, 'B' DESTINATION, 3 ORDERING FROM DUAL UNION ALL
  SELECT 1 ID, 'B' ORIGIN, 'C' DESTINATION, 4 ORDERING FROM DUAL UNION ALL
  SELECT 1 ID, 'C' ORIGIN, 'A' DESTINATION, 5 ORDERING FROM DUAL
)
  SELECT T.ID
       , T.ORIGIN
       , T.DESTINATION
       , T.ORDERING
       , LAG (T.ORIGIN, T.ORDERING -1, 0) OVER (PARTITION BY T.ID
                                                    ORDER BY T.ID
                                                           , T.ORDERING) FIRST_ORIGIN_OF_GROUP
    FROM T
ORDER BY T.ID
       , T.ORDERING

Hello

Here's one way:

WITH     got_first_origin     AS
(
     SELECT     id, origin, destination, ordering
     ,     FIRST_VALUE (origin) OVER ( PARTITION BY  id
                                      ORDER BY         ordering
                                 ) AS first_origin
     FROM    t
)
SELECT     id, origin, destination, ordering
,     COUNT ( CASE
                    WHEN  origin = first_origin
              THEN  1
                END
           )     OVER ( PARTITION BY  id
                       ORDER BY      ordering
               ) AS numbering
FROM     got_first_origin
;

This assumes that the combination of id and order is unique. Within an id, you place your order does not have to be consecutive integers, or something like that.

Analytical functions cannot be nested (the argument of the function of COUNTY anlytic can not call the analytical FIRST_VALUE function); The subquery is necessary.
You could do something with a LAG, as you have tried, rather than FIRST_VALUE, but you would still need a subquery, for the same reason.

Tags: Database

Similar Questions

  • How to divide the lines based on two columns...

    Hi all...
    I have a requirement.

    I have the product, sale, purchase price column... (total of three columns) in a data set.

    my data set is in such a way that... If the sale price is... There is no purchase price and vice versa...

    I need too present in a report, two tables:
    Table 1 includes only the elements of retail price
    Table 2 should only contain the right elements.
    Please see the below photo for good understanding...
    Is this feasible? Where do we need to impose a condition? I tried to impose a condition, but it doesn't seem to work

    http://i51.Tinypic.com/29xfdc6.jpg


    Help, please.

    Can you send me the xml file and the model of [email protected]? I can try to help.

    Have you tried to filter the records by the sale or purchase price is not equal to null column price?

    Thank you
    Bipuser

  • Single line based on two columns and a single column

    Dear members,

    I have a table that contains duplicate rows, for which a request should be able to extract the unique row in the table. Here the unique is not based on a single column, but it should be in two columns and also check on the uniqueness on a column.

    create table addr (varchar2 (10) firstname, lastname varchar2 (10), area varchar2 (3));

    insert into values addr ('bob', 'james', 1');
    insert into values addr ('bob', 'james', 1');

    insert into values addr ('harry', 'bert', ' 1');
    insert into values addr ('jimmy', 'bert', ' 1');

    insert into values addr ('sam', 'mac', '1');
    insert into values addr ('sam', 'Knight', '1');

    insert into values addr ('tom', 'sand', '1');
    insert into values addr ("cat", "mud", "1");


    The query output must contain 3 lines.

    Bob - james
    Harry - bert or jimmy - bert [or the other of them], but not both
    -Mac or sam - Sam Knight [or the other of them], but not both
    Tom - sand
    Cat - mud

    SELECT firstname, lastname as total area WHERE addr = '1' GROUP by firstname, lastname; It takes no duplication of single column...

    Any suggestions...
    SQL> with t_data
    as
    (
    select 'bob' as firstname, 'james' as lastname, '1' as area from dual union all
    select 'bob', 'james', '1' from dual union all
    select 'harry', 'bert', '1' from dual union all
    select 'jimmy', 'bert', '1' from dual union all
    select 'sam', 'mac', '1' from dual union all
    select 'sam', 'knight', '1' from dual union all
    select 'tom', 'sand', '1' from dual union all
    select 'cat', 'mud', '1' from dual
    )
    SELECT
            firstname,
            lastname,
            area
    FROM
            (
                    SELECT
                            t.*,
                            row_number() over(partition BY firstname order by 1) rn,
                            row_number() over(partition BY lastname order by 1) rn1
                    FROM
                            t_data t
            )
    WHERE
            rn     = 1
    AND rn1 =1 ;  
    
    FIRSTNAME       LASTNAME        AREA
    --------------- --------------- ----------
    bob             james           1
    cat             mud             1
    jimmy           bert            1
    sam             knight          1
    tom             sand            1
    
    SQL>
    
  • SQL query * I want to insert a file column in the other table based on two columns

    I want to insert a column records to another table based on two columns

    the query below does not work

    insert into def_ver_lnki_p (job_name)

    Select def_job.job_name

    from def_job where def_job.job_id = def_ver_lnki_p.job_id and def_job.table_id = def_ver_lnki_p.table_id;

    Just to correct sql

    Update

    def_ver_lnki_p define job_name =)

    Select def_job.job_name

    of def_job

    where def_job.job_id = def_ver_lnki_p.job_id

    (and def_job.table_id = def_ver_lnki_p.table_id);

  • How to update a record, if it is a composite key (primary key is based on two columns) in the OPS? page?

    Hello

    Actually I got to know this update of a record must be made when the primary key is based on two columns.

    Previously, I used SPEL for a column. It was working fine.

    But I came to know that regardless of the folder I try to update, it was the update page is coming up with the first combination of the record.

    As if RC 131

    RC 124

    RC 125 are the records.

    If I click on update of RC 124 also, the update page is coming up with RC 131 values as it is the first.

    So I understand that the SPEL should be based on two parameters, because it is a composite key.

    How to do?

    Please let me know if you don't understand the problem.

    Thank you.

    Wow!

    I got it.

    Thank you brothers, always a hope that you could help me

  • 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.

  • Change the background color of line based on a column value

    Hello

    in a report, I want to change the record to the red background color if the value of a specific column is 'NO' in the folder. Please help me.

    Thank you.

    Try this

    I had this same problem and found that by using the following only highlighted the background of the text and not the entire cell

    
    

    I then swapped the use of div (as below) and this has solved my problem

    select test_table.column1 as "yes_no",
          CASE WHEN column2='NO'
             THEN '
    '||column2||'
    ' ELSE column2 END "column2" from test_table

    This has been tested in 3.2.1 - should work with other versions so

    Gareth

  • Select all the lines of a specific column

    Hello

    I have a complete excel data file and I want to display in the control table labview

    the number of rows in the excel file is not defined, but I know what colume there is.

    so now im using report generation tools read my excel file by setting the line position and colume, but if I put '-1' on the two lines which is of all package read and the column position, an error occurs.

    any way to solve?

    64864050 wrote:

    Hello

    I have a complete excel data file and I want to display in the control table labview

    the number of rows in the excel file is not defined, but I know what colume there is.

    so now im using report generation tools read my excel file by setting the line position and colume, but if I put '-1' on the two lines which is of all package read and the column position, an error occurs.

    any way to solve?

    There is no problem with the index of line-1, as long as you read one column.

    To read several columns, you must set the start and the end of the table block. Since you know your number in the column, simply determine the line number. You can use the Row.vi last Excel get, as shown below.

  • Setting the background color of the line according to a column value

    Hello

    in a classic report, I want to change the record to the red background color if the value of a specific column is 'NO' in the folder. Please help me.

    Apex 4.1
    RDBMS 11.2

    Thanks in advance
    lukx

    Hello

    Use custom report template. This post might help
    Re: Highlight a line in a tabular form, based on a column in the row

    Kind regards
    Jari

    http://dbswh.webhop.NET/dbswh/f?p=blog:Home:0

  • selection of lines corresponding to two columns with two entry tables

    I have a table with two columns int primary key tab1. id1 and id2.

    Now I have two created two tables of figures: array1 {1, 2, 3,...} and {0, 1, 0,...} array2 Arrays have the same length.

    I want to do a select:
    Select * from tab1 where tab1.id1 = array1.column_value and tab1.id2 = array2.column_value;

    If I want to receive 13 lines with this request, I had 169 because it tries to match all the different combinations in the tables.

    If I do it with a loop for:

    for i of array1
    loop
    Select * from tab1 where tab1.id1 = array1 (i) and tab1.id2 (i);
    end

    That should illustrate what I want to do, I just thought it would be faster if I could do it without a loop, do not use the pl/sql. Can it be done? If not, should I use a loop for with bulk collect somehow?
    SQL> with t as (
      select 1 id1, 0 id2 from dual union all
      select 2, 0 from dual union all
      select 3, 0 from dual union all
      select 4, 1 from dual
    )
    --
    --
    select id1, id2
    from t,
         (select rownum rn1, a1.* from table (sys.odcinumberlist (1, 2, 3, 4)) a1) a1, --- array1
         (select rownum rn2, a2.* from table (sys.odcinumberlist (0, 1, 0, 1)) a2) a2  --- array2
    where (id1, id2) in ( (a1.column_value, a2.column_value)) and rn1 = rn2
    /
           ID1        ID2
    ---------- ----------
             1          0
             3          0
             4          1
    
    3 rows selected.
    
  • Query SQL to split the lines based on the amount

    I have the data in the following format in the table.

    ORDER_ID PRODUCT_ID QUANTITY

    O1 A1   3

    I need to write the sql query to divide the data in following format:

    ORDER_ID PRODUCT_ID QUANTITY

    O1 A1   1

    O1 A1   1

    O1 A1   1

    Query must split the data based on the value in the quantity column.

    Thank you

    Developer

    Hello

    create table order_items (
      order_id varchar2(2),
      product_id varchar2(2),
      quantity number
    )
    ;
    insert into order_items values ('O1', 'A1', 3)
    ;
    -- Recursive Subquery Factoring
    with item(order_id, product_id, quantity) as (
      select
        order_id, product_id, quantity
      from order_items
      union all
      select
        order_id, product_id, quantity - 1
      from item
      where quantity > 1
    )
    select
      order_id, product_id, 1 quantity
    from item
    order by order_id
    ;
    drop table order_items purge
    ;
    
    Table ORDER_ITEMS created.
    
    1 row inserted.
    
    OR PR   QUANTITY
    -- -- ----------
    O1 A1          1
    O1 A1          1
    O1 A1          1
    
    Table ORDER_ITEMS dropped.
    
  • How to trigger the new numbering of the lines in a group?

    Suppose we have an ORDER_DETAILS table with columns (ORDER_ID, LINE_NUMBER, DESCRIPTION).

    We have these lines:

    order_id, line_number, description

    17: 1, "A".

    17.2, 'G '.

    17.3, 'R '.

    17.4, "Q".

    Next, it inserts a new line

    17.2, 'K '.

    How to trigger the renumbering?

    This should be the result after renumbering.

    17: 1, "A".

    17.2, 'K '.

    17.3, 'G '.

    17.4, 'R '.

    17.5, "Q".

    Then we move the line 5 to line 2.

    How to trigger the renumbering?

    This should be the result after renumbering.

    17: 1, "A".

    17.2, "Q".

    17.3, 'K '.

    17.4, 'G '.

    17.5, 'R '.

    This question is related to another discussion:

    https://forums.Oracle.com/thread/1088303

    create the order_details table

    (number of order_id,

    number of line_number,

    Description varchar2 (30)

    )

    rowdependencies;

    Start

    insert into values order_details (17, 1, 'A');

    insert into values order_details (17.2, 'G');

    insert into values order_details (17.3, 'R');

    insert into values order_details (17.4, 'Q');

    end;

    Select * from order_details by line_number

    ORDER_ID LINE_NUMBER DESCRIPTION
    17 1 A
    17 2 G
    17 3 R
    17 4 Q

    create or replace renumber procedure (number p_order_id)

    Start

    Update order_details od

    Set line_number = (select rn

    from (select rowid, RID,)

    / * for autocommit APEX active, etc. * / row_number() on rn (order of line_number, ora_rowscn desc)

    / * for autocommit = OFF favorite above row_number() (line_number order, nulls first ora_rowscn) rn * /.

    of order_details

    where order_id = p_order_id

    )

    where rid = od.rowid

    )

    where order_id = p_order_id;

    end;

    Start

    insert into values order_details (17.2, 'K');

    RENUMBER (17);

    end;

    Select * from order_details by line_number

    ORDER_ID LINE_NUMBER DESCRIPTION
    17 1 A
    17 2 K
    17 3 G
    17 4 R
    17 5 Q

    Start

    Update order_details

    Line_number = 2 set

    where order_id = 17

    and line_number = 5;

    RENUMBER (17);

    end;

    Select * from order_details by line_number

    ORDER_ID LINE_NUMBER DESCRIPTION
    17 1 A
    17 2 Q
    17 3 K
    17 4 G
    17 5 R

    Concerning

    Etbin

  • SQL query: only the numbers in the line of the somme - need help

    Pls see data below. I want to display only 8 lines and lines of BRH NCC should be merged and displayed as - BRH 98 2 instead of 2 rows. The rest of the lines must remain as it is. Help, please.


    NCC SERVICES MANUFACTURING
    ASL 138 NA
    BRH 98 NA
    BRH NA 2
    C10000 NA 0
    C40000 NA 0
    DAO NA 10
    E10000 NA 0
    I10000 NA 0
    M10000 NA 0

    Published by: auxilia on November 7th, 2009 06:25
    with t as (
               select 'ASL' ccn,'138' services,'NA' manufacturing from dual union all
               select 'BRH','98','NA' from dual union all
               select 'BRH','NA','2' from dual union all
               select 'C10000','NA','0' from dual union all
               select 'C40000','NA','0' from dual union all
               select 'DAO','NA','10' from dual union all
               select 'E10000','NA','0' from dual union all
               select 'I10000','NA','0' from dual union all
               select 'M10000','NA','0' from dual
              )
    select  ccn,
            nvl(max(case services when 'NA' then null else services end),'NA') services,
            nvl(max(case manufacturing when 'NA' then null else manufacturing end),'NA') manufacturing
      from  t
      group by ccn
    /
    
    CCN    SERVICES             MANUFACTURING
    ------ -------------------- --------------------
    E10000 NA                   0
    C10000 NA                   0
    BRH    98                   2
    M10000 NA                   0
    C40000 NA                   0
    ASL    138                  NA
    I10000 NA                   0
    DAO    NA                   10
    
    8 rows selected.
    
    SQL>   
    

    SY.

    Published by: Solomon Yakobson on November 7th, 2009 06:46

  • Automate the cells of the filter to another table based on two columns

    Hello

    What I'm trying to do is: filter multiple cells to a new table. In this example, I want that all students (column 'lidnr', 'city', then the first 7 columns) with 1 teacher in the 'First' column to be copied into the table 'teacher 1'. The same goes for students per 1 teacher in the column "second". I know it can be done with the help of the filter and the copy paste, but I would like to know if this can be automated, because there are many more tables in my actual document.

    I've been using maybe to think the search formula but he can't give me more than one result, the result must be unique.

    Thank you very much for your help.

    Kind regards

    Kim

    Hi Kim,

    The usual way to do this is to add an index for each table for which column the data is to be copied.

    Here is an example of preliminary. Improvements after return from the light of day.

    Triangles of error on the last row of T1 are thanks to the formula out of data to search for. Easily fixed.

    Order of the names is different from your example, which is grouped by day numbers. Build lists in the desired order, rethinking the index calculations.

    Course index calculations use the formula below, entered in Table 1::L2 and thre to the rest of the blue full cells filled.

    L2: = IF (OR (RIGHT($H2,1) = RIGHT (L$ 1.1) RIGHT($J2,1) = RIGHT (L$ 1.1)), MAX(L$1:L1) + 1"," ")

    Data recovery on the Table T1 is well done by the following formula, entered in A2 and down and B2 and filled and down-filled.

    A2: = INDEX (array 1::A, CORRESPONDENCE (LINE (−1, 1 Table): $L, 0))

    B2: = INDEX (array 1::B, CORRESPONDENCE (LINE (−1, 1 Table): $L, 0))

    More tomorrow (actually, later today).

  • Looking for 6 numbers on a line in a varchar column

    The database is Oracle 9 so I can't use regular expressions. :( I have a column varchar2 with long string as inputs:


    COLUMN_X
    --------------------------------------------------------------------------------------------------------
    Stuff/stuff_start1.4_yes_208336_why_1.0_D9
    more_stuff / running_start_env_208398_wonder_2.0_D98
    yetmore_stuff2.9/workistedious_start_wordup_256336_why_1.7_D98658
    yetmore_stuff/workistediouswordy_256336_I_am_green



    I need to get out just the number series when there are 6 numbers in a row so I can them are aligned with the other rows in the table with the same 6 digits. If I shoot only numbers I sometimes get too many numbers, and it is also possible that I don't want numbers will appear before or after the 6 figures I'm after. How can I select according to the COLUMN_X data above and get only the numeric characters when there is 6 of them in order?

    COLUMN_X
    ___________
    208336
    208398
    256336
    256336



    CREATE TABLE dt_test (COLUMN_X varchar2 (50))
    /

    insert into dt_test values ('stuff/stuff_start1.4_yes_208336_why_1.0_D9')
    /
    insert into dt_test values ('more_stuff / running_start_env_208398_wonder_2.0_D98')
    /
    insert into dt_test values ('yetmore_stuff2.9/workistedious_start_wordup_256336_why_1.7_D98658')
    /
    insert into dt_test values (' yetmore_stuff/workwordy_256336_I_am_green')
    /

    Thanks in advance

    The database is Oracle 9 so I can't use regular expressions.

    You can with owa_pattern ;)

    You can create a function and use it as

    SQL> create or replace function match (line varchar2, pat varchar2, flags varchar2 default null)
       return sys.dbms_debug_vc2coll
       pipelined
    as
       b              boolean;
       backrefs       owa_text.vc_arr;
       r              sys.dbms_debug_vc2coll;
    begin
       b   := owa_pattern.match (line, pat, backrefs, flags);
    
       for i in 1 .. backrefs.count
       loop
          pipe row (backrefs (i));
       end loop;
    
       return;
    end match;
    /
    Function created.
    
    SQL> select * from dt_test, table (match (column_x, '(\d{6})'))
    /
    COLUMN_X                                                               COLUMN_VALUE
    ---------------------------------------------------------------------- ------------------------------
    stuff/stuff_start1.4_yes_208336_why_1.0_D9                             208336
    more_stuff/running_start_env_208398_wonder_2.0_D98                     208398
    yetmore_stuff2.9/workistedious_start_wordup_256336_why_1.7_D98658      256336
    yetmore_stuff/workwordy_256336_I_am_green                              256336                        
    
    4 rows selected.
    

Maybe you are looking for