Need query to add duplicates

Guys,
I've been trying to solve the shortest route problem and got stuck in calculating the cumulative distance in a hierarchial query. I've got some idea to deal with it like
sum(distance) over (order by level rows level preceding)But in order for this to happen I may be needing duplicate records. Anyway, let me get straight to what I need.
I have table data like this.
col1      col2       rown
1         10         1
2         20         2
3         30         3
4         40         4
4         70         5
5         80         6
3         90         7
5         95         8I want the result to be like this.
col1      col2
1         10
2         20
3         30
4         40
1         10
2         20
3         30
4         70
5         80
1         10
2         20
3         90
1         10
2         20
3         30
4         70
5         95Please observe that when filling with duplicates, if there are 2 or more occurances of the same, it should be the latest(maximum rown in all). For eg: To fill for 5---95, there are two 3s (3---30 and 3---90) and two 4s(4---40 and 4---70). The latest need to be taken meaning 3---90 and 4---70 are to be taken. I hope I made it clear. Please let me know if you need any further information.
Cheers
Sarma.

Thank you Etbin.
It was line # 16 that was missing. Now it looks okay.
SQL> select finaal.col1, finaal.col2
  2  from (select col1, col2, rown,
  3               row_number() over (partition by col1, rown
  4                                  order by col2 desc) rn
  5        from ( select b.col1 col1,
  6                      b.col2 col2,
  7                      a.rn rown
  8               from sample b, (select col1, col2, rown, rn
  9                               from (select col1, col2, rown, rownum rn,
10                                            lag(col1) over (order by rown) prev
11                                     from sample
12                                    )
13                               where col1 - prev <> 1
14                              ) a
15               where b.col1 < a.col1
16               and b.rown <= a.rown
17               union all
18               select col1,
19                      col2,
20                      rown
21               from sample
22             )
23         ) finaal
24  where finaal.rn = 1
25  order by finaal.rown, finaal.col1
26  /
      COL1       COL2
         1         10
         2         20
         3         30
         4         40
         1         10
         2         20
         3         30
         4         70
         5         80
         1         10
         2         20
         3         90
         1         10
         2         20
         3         90
         4         70
         5         95
         1         15
         1         15
         2         20
         3         90
         4         80
         1         15
         2         20
         3         90
         4         80
         5         95
         6         99
         1         15
         2         20
         3         33
         1         15
         2         20
         3         90
         4         80
         5         97
36 rows selected.
SQL>Cheers
Sarma.

Similar Messages

  • AF:QUERY : JBO-25058 when we add duplicate instance of multi select enabled and search

    Hi Getting JBO-25058 error when we add duplicate instance of multi select enabled attribute from Add Fields and click on search Our requirement is to add validation if search panel has duplicate instance of multi select enabled attribute but this error comes before control goes to Managed bean. Kinldy help. Typical message: JBO-25058: Definition _sel__vci_lov_LOV_ForecastType_ActiveRevnForecastVC_vcrow2097_ForecastType_vcrow2097_ForecastType_itemHolder_vcrow1_ForecastType of type Attribute is not found in __LOCAL_VIEW_USAGE_sessiondef_ForecastAM_oracle_apps_sales_salesForecasting_forecasts_forecastService_applicationModule_ForecastAM_ForecastOverviewPageVO_vcrow2097_ForecastType_itemHolder_ForecastTypeLookupPVO_findByVC_8fc_LOV_ForecastType__lov__filterlist__vcr___.
    Regards
    Rajesha

    Sarah,
    I could reproduce the error.
    However, I created a drag-and-drop page fragment with quick search and table, and modified the adfc-config-EmpVw task flow to reference the drag and drop page:
    <view id="EmpVwTable">
    <page id="__120">/EmpvwDD.jsff</page>
    </view>
    And then I still get the same error, so this looks like an ADF bug to me.
    I also created a stand-alone jspx page using drag and drop, same error as well. I suggest you make a testcase without JHeadstart and contact Oracle Support.
    Steven Davelaar,
    JHeadstart Team.

  • Query to add a column in between existing cols of a table?

    HI All,
    I have two questions.
    1. Query to add a column in between existing cols of a table? (not at the end. This is view of an order of output fields in a report)
    2. How do I swap the contents of two columns in a table. Suppose in a table tab there are 2 cols , col1,col2 populated with some data.
    I need a query(probably) to swap the col1 and col2 values . NOT AS A RESULT SET, BUT IT NEEDS TO GET CHANGED IN THE TABLE.
    Please help !

    > 1. Query to add a column in between existing cols of
    a table? (not at the end. This is view of an order of
    output fields in a report)
    Not really sensible ito DBMS - it does not care how you want to view the data. The sequence and formats of columns are what you/the application need to specify in the SQL's projection clause.
    Also keep in mind to achieve this, the DBMS will need to rewrite the entire table to fit this new column in-between existing columns. This is not the best of ideas.
    The projection of rows is dealt with SQL statements - not with the physical storage implementation.
    > 2. How do I swap the contents of two columns in a
    table. Suppose in a table tab there are 2 cols ,
    col1,col2 populated with some data.
    I need a query(probably) to swap the col1 and col2
    values . NOT AS A RESULT SET, BUT IT NEEDS TO GET
    CHANGED IN THE TABLE.
    This seems to work:
    SQL> create table foo_tab( c1 varchar2(10), c2 varchar2(10) );
    Table created.
    SQL> insert into foo_tab select TO_CHAR(rownum), TO_CHAR(object_id) from user_objects where rownum < 11;
    10 rows created.
    SQL> commit;
    Commit complete.
    SQL> select * from foo_tab;
    C1 C2
    1 55816
    2 55817
    3 55818
    4 55721
    5 105357
    6 105358
    7 105359
    8 105360
    9 105361
    10 60222
    10 rows selected.
    SQL> update foo_tab set c1=c2, c2=c1;
    10 rows updated.
    SQL> select * from foo_tab;
    C1 C2
    55816 1
    55817 2
    55818 3
    55721 4
    105357 5
    105358 6
    105359 7
    105360 8
    105361 9
    60222 10
    10 rows selected.
    SQL>

  • Query to add responsibility to multiple Users.

    Hi All,
    I need a query to add a newly created RESPONSIBILITY to multiple Oracle Apps Users (>1000) through sql query , not through Data Loader. Any help in this regard will be highly appreciated.
    Please list out the tables on which the INSERT (or other) statement need to be performed for acheiving the above mentioned purpose.
    Regards,
    Imran

    Hi,
    I believe you need to read the list of username from FND_USER table and write a script to add this new responsibility for each user.
    How to use FND_USER_PKG.AddResp
    Re: How to use FND_USER_PKG.AddResp
    Regards,
    Hussein

  • Query to eliminate duplicates

    I have a table that lists some data. Every day should have one and only one record per organization. I am trying to weed out the duplicates in one query. It doesn't matter which of the duplicates gets deleted, so I could use a (where rownum = 2) clause, but I'm not sure how to get there.
    This query exposes the duplicates:
    select DATE_INDEX, ORG_ID
      from STAFFING_TURNOVER
    group by DATE_INDEX, ORG_ID
    having COUNT(ID) > 1
    order by DATE_INDEX, ORG_IDCan someone help me turn this into a delete query that deletes the second record? Because it is based on a combination of date + organization, the syntax for a subquery-based delete is escaping me.

    Delete from STAFFING_TURNOVER Where RowID in
    (Select Min(RowID) From STAFFING_TURNOVER Group by DATE_INDEX, ORG_ID Having Count(1) > 1)
    Oops, someone posted the same thing.
    If you need to you can execute it more than once if you have more than one. You can also perform an elegant minus query or use a Sub-query to ID the dups. The problem will be performance and depend on how much data you have.
    Edited by: Dev on Dec 1, 2010 3:25 PM

  • Query to find duplicate lows

    Dear all,
    i have a table
    create table attendance(dept_id number,att_date date,att_kind);
    all the three columns i want to put as a primary key,
    there is a data in the table.
    how would i check through a query that the table has duplicate values or not?
    in other words , i want to query just the duplicate records so i delete them manually.
    thanks & Regards

    You do it like this.
    SQL> select * from attendance;
       DEPT_ID ATT_DATE    ATT_KIND
           100 20-FEB-10          1
           100 20-FEB-10          1
           200 20-FEB-10          1
           200 20-FEB-10          2
    SQL> create table exceptions(row_id rowid,
      2   owner varchar2(30),
      3   table_name varchar2(30),
      4    constraint varchar2(30));
    Table created.
    SQL> alter table attendance add constraint att_pk  primary key (dept_id, att_date, att_kind) exceptions into exceptions;
    alter table attendance add constraint att_pk  primary key (dept_id, att_date, att_kind) exceptions into exceptions
    ERROR at line 1:
    ORA-02437: cannot validate (TEST.ATT_PK) - primary key violated
    SQL> select * from exceptions;
    ROW_ID             OWNER                          TABLE_NAME                     CONSTRAINT
    AAAC/yAAGAAAAAOAAB TEST                           ATTENDANCE                     ATT_PK
    AAAC/yAAGAAAAAOAAA TEST                           ATTENDANCE                     ATT_PK
    SQL> select * from attendance where rowid in (select row_id from exceptions);
       DEPT_ID ATT_DATE    ATT_KIND
           100 20-FEB-10          1
           100 20-FEB-10          1
    SQL>Asif Momen
    http://momendba.blogspot.com

  • Problem loading Query Designer Add-in

    Hi All,
    Can anyone please help me on my below issue.
    Whenever user is trying to start a new query in Essbase , he is getting below error message. It will not allow user to build a fresh Essbase pull
    Problem loading Query Designer Add-in. Workbook Activate,open & close operations may not be fully propogated
    Thanks in advance.
    Regards
    Vishal

    Hi Vishal,
    1) Try to remove the addin and then add again.(Before doing this close all the excel process from the task Manager)
    2) Check weather essexcln.xll and essxleqd.xla are corrupted.

  • My external HD of 1TB is not being recognized by the iMac and when after a long waiting period it does, the whole computer becomes extremely slow. Do I need any particular add-on to make it workable?

    My external HD of 1TB is not being recognized by the iMac and when after a long waiting period it does, the whole computer becomes extremely slow. Do I need any particular add-on to make it workable?
    Before I updated to OS X Mavericks I was using another external disk that was getting disconnected ocasionally so I bought a new ADATA HD710 portable 1TB Drive and moved the files to it. This new drive which is supposed to be OS X Mavericks compatible takes minutes to show the files especially when opening second and third level folders which has made mu iMac unusable
    What should I do?

    Your Mac should be able to recognize many different types of drives (including Windows-formatted drives) immediately for Reading, provided they are in good working condition and not damaged. There should be no need to transfer to a different type of drive.
    If there is no recognition of this drive, see if it can be seen in Disk Utility.
    If your aim is to transfer the data on it DO NOT use ERASE or PARTITION as these will completely erase all the information on the Drive.
    If the drive is present while the Mac sleeps, it is likely to drop out and not be seen any longer, especially if it is a USB-powered drive.

  • Need query to find out whether exactly 2 columns are used in any index ?

    Suppose i have two columns ID and Status (or any number of columns ) belongs to table customer...
    Now I want to create below index
    Ex. create index test1 on customer (ID , Status )
    But before creating this index i want to check that whether there is already index on these 2 columns ? May be by other name ?
    Need query for this.
    Plz help.

    Hi Shubhangi,
    Your requirement is very difficult to fulfill , i have made an attempt to replicate your reqrmnt & write a query wherein you need to compromise on few things , let us see if it works for you
    Supply table_name/owner & Number of column on which you want to find indexes
    e.g.
    select distinct INDEX_NAME,column_name
    from dba_ind_columns
    where index_name in (select distinct index_name
    from dba_ind_columns
    where table_name='&table_name'
    and table_owner='&owner'
    having count(column_name)=&column_count group by index_name);
    Enter value for table_name: ADDRESS
    Enter value for owner: ASAP
    Enter value for column_count: 2
    INDEX_NAME COLUMN_NAME
    FKIDX_AD__SF_ST_FO SF_STRUC_FORMAT_NM
    FKIDX_AD__SF_ST_FO SF_TYPE_NM
    SQL> /
    Enter value for table_name: ADDRESS
    Enter value for owner: ASAP
    Enter value for column_count: 3
    INDEX_NAME COLUMN_NAME
    FKIDX_AD__SF_ST_FO_1 POSTAL_CD
    FKIDX_AD__SF_ST_FO_1 SF_STRUC_FORMAT_NM
    FKIDX_AD__SF_ST_FO_1 SF_TYPE_NM
    Thanks,
    Ajay More
    http://moreajays.blogspot.com

  • Need query to find rows

    hi
    i need query to find out for one perticular row from parent table ,,which references how many rows in how many child tables , and child to child tables. in herirachy fashion.
    if anybody know please help me

    I guess it would be difficult to come up with a generic script to get answer that can help in all situations...you have to see the table definitions in your schema and try...like in case of emp and dept table,
    select * from dept where deptno in
    (select deptno,count(*) from emp group by deptno having count(*)>1)
    (this gives rows from dept table where , for corresponding deptno there are more than one rows in emp table).
    Thanks
    Nirav

  • An alert pops up and asks if I want to merge my contacts with Yahoo. If I merge, will it add duplicates?

    An alert pops up and asks if I want to merge my contacts with Yahoo.
    If I merge, will it add duplicates, if there are some?
    I don't want duplicates.
    If it will add duplicates, how do I tell yahoo to quit asking me if I want to merge.

    *Firefox 4 versions and later do not run on a PowerPC Mac.
    *Firefox 4 and later require at least OS X 10.5 and an Intel Mac.
    * http://www.mozilla.com/firefox/5.0/system-requirements/
    For an unofficial Firefox 4 compatible version (TenFourFox) that runs on PowerPC Macs with OS X 10.4.11 or OS X 10.5.8 you can look at:
    * http://www.floodgap.com/software/tenfourfox/
    * http://code.google.com/p/tenfourfox/
    Firefox 3.6.x (Mac OS X 10.4 and later) can be found here:
    * http://www.mozilla.com/en-US/firefox/all-older.html

  • Need Query - join in same table

    I need query for following criteria,
    Table : test
    No     Order
    1     a
    1     b
    1     c
    2     a
    2     b
    2     d
    3     e
    3     f
    3     g
    3     h
    1     f
    2     f
    Consider the above table,
    1)     I will give input order as a,b: It should return No 1,2
    No     Order
    1     a
    1     b
    1     c
    2     a
    2     b
    2     d
    3     e
    3     f
    3     g
    3     h
    1     f
    2     f
    2)     I will give input order as f,g,h: It should return No 3
    No     Order
    1     a
    1     b
    1     c
    2     a
    2     b
    2     d
    3     e
    3     f
    3     g
    3     h
    1     f
    2     f
    Please give me the query which will give above result.
    Thanks

    I am not sure I understand you, but it may be this
    with test as (
    select 1 N, 'a' Ord from dual union all
    select 1,'b' from dual union all
    select 1,'c' from dual union all
    select 2,'a' from dual union all
    select 2,'b' from dual union all
    select 2,'d' from dual union all
    select 3,'e' from dual union all
    select 3,'f' from dual union all
    select 3,'g' from dual union all
    select 3,'h' from dual union all
    select 1,'f' from dual union all
    select 2,'f' from dual )
    select N from test tp where Ord = 'a'
    intersect
    select N from test tp where Ord = 'b';
    with test as (
    select 1 N, 'a' Ord from dual union all
    select 1,'b' from dual union all
    select 1,'c' from dual union all
    select 2,'a' from dual union all
    select 2,'b' from dual union all
    select 2,'d' from dual union all
    select 3,'e' from dual union all
    select 3,'f' from dual union all
    select 3,'g' from dual union all
    select 3,'h' from dual union all
    select 1,'f' from dual union all
    select 2,'f' from dual )
    select N from test tp where Ord = 'f'
    intersect
    select N from test tp where Ord = 'g'
    intersect
    select N from test tp where Ord = 'h';

  • Need Query to display

    Hi Folks,
    I need query for displaying following:
    Adjusted A/R Balance:-
    Sum of all open Receivables transactions for the Bill To address for a :Particular_Customer. Total of all open items (Invoice, Credit Memos, CLAIMS, ON ACCTS, UNAPPLIED, DEBIT MEMOS) PLUS any orders that have been approved but not yet invoiced.
    ================================================================================
    Credit Tolerance:-
    Percentage of Credit Limit that is the allowed tolerance for exceeding the credit limit. Determined by the following calculation: (Exposure Credit Limit ((Credit Tolerance + 100)* Credit Limit / 100) – Credit Limit)/Credit Limit.
    would be very greatful if someone can help on same.
    Thanks in advance.
    Regards,
    gvk.

    another example would be:
    SQL> with emp1 as
      2  (select 10 emp_id, 'AAAA' emp_name from dual
      3   union all
      4   select 22 emp_id, 'BBBB' emp_name from dual
      5   union all
      6   select 15 emp_id, 'BBBB' emp_name from dual
      7   union all
      8   select 17 emp_id, 'cccc' emp_name from dual
      9   union all
    10   select  4 emp_id, 'DDDD' emp_name from dual
    11   union all
    12   select  5 emp_id, 'EEEE' emp_name from dual
    13   union all
    14   select 53 emp_id, 'EEEE' emp_name from dual)
    15  select emp_id id, emp_name name from emp1;
            ID NAME
            10 AAAA
            22 BBBB
            15 BBBB
            17 cccc
             4 DDDD
             5 EEEE
            53 EEEE
    7 rows selected.
    SQL> with emp1 as
      2  (select 10 emp_id, 'AAAA' emp_name from dual
      3   union all
      4   select 22 emp_id, 'BBBB' emp_name from dual
      5   union all
      6   select 15 emp_id, 'BBBB' emp_name from dual
      7   union all
      8   select 17 emp_id, 'cccc' emp_name from dual
      9   union all
    10   select  4 emp_id, 'DDDD' emp_name from dual
    11   union all
    12   select  5 emp_id, 'EEEE' emp_name from dual
    13   union all
    14   select 53 emp_id, 'EEEE' emp_name from dual)
    15  select row_number() over (order by emp_name, emp_id) rn,
    16         emp_id   id,
    17         emp_name name
    18    from emp1;
            RN         ID NAME
             1         10 AAAA
             2         15 BBBB
             3         22 BBBB
             4          4 DDDD
             5          5 EEEE
             6         53 EEEE
             7         17 cccc
    7 rows selected.
    SQL>

  • Need Query for empty partitions

    I am having nearly 700 partitions for a table.Now i want to find out only the empty partitions.I need query for that.
    Thankx..

    Not the most elegant solution, but it works:
    declare
    rc number;
    str varchar2(200);
    begin
    for i in (select table_owner, table_name, partition_name from dba_tab_partitions) loop
    str := 'select count(*) from ' || i.table_owner || '.' || i.table_name || ' partition (' || i.partition_name || ')';
    execute immediate str into rc;
    if rc = 0 then
    dbms_output.put_line(i.table_owner || '.' || i.table_name);
    end if;
    end loop;
    end;

  • Help Please. Receiving thousands of messages saying that I need to buy add-on.

    Hi, I am receiving thousands of messages saying that I need to buy add-on without reason, it happened to other people, EE can go to EE website. http://community.ee.co.uk/t5/The-EE-Network/4000-EE-texts-saying-quot-To-use-data-on-your-phone-you-ll-need/m-p/304390/highlight/false#M43457 http://community.ee.co.uk/t5/The-EE-Network/Why-do-i-keep-getting-to-use-data-on-your-phone-you-l-need-add/m-p/271155/highlight/false#M33503  I have been calling EE for 3 hours, EE is not able to deal with that. EE just try to make my mobile phone responsible for that. Awful services and support. Is there help here?

    Hi 
    Sorry to hear you are having this problem.
    It needs to be investigated by our tech team.
    If you call 150 and chose option 1 then option 3 then option 1.
    Hope this helps!
    Thanks. 

Maybe you are looking for