简体   繁体   中英

Delete duplicates based on Subquery

Hi I have a table with following records

    SEQ_VAL, ACC, TERR, SEG2, ORG_ID      
    273480  673015  59027   CA  521
    273481  673015  44027   US  521
    287582  323181  44027   US  521
    351420  323181  59027   CA  521
    287589  3228    44027   US  521
    287977  3229    44027   CA  521
    90990   6768    586709   US  521
    90966   6768    586709   US  521

I need the following output. I basically need to delete the duplicate records for each acc and org_id.For each acc and org_if if multiple records exists with seg2 val CA and US, I only needs to keep US. If a single record exists for each of CA and US, I need to keep them aswell.

    SEQ_VAL, ACC, TERR, SEG2, ORG_ID      

    273481  673015  44027   US  521
    287582  323181  44027   US  521
    287589  3228    44027   US  521
    287977  3229    44027   CA  521
    90990   6768    586709   US  521

I am using below delete statement and its throwing the following error.

SQL Error: ORA-01732: data manipulation operation not legal on this view

Delete from (    
    SELECT abc.*,
       ROW_NUMBER ()
           OVER (PARTITION BY  ACC, ORG_ID
              ORDER     BY SEG2  DESC)
       seq_no
  FROM abc 
 ) a 
 where a.seq_no = 2

basing on your data this query works fine

Delete 
    from
             Table
WHERE RN 
    IN  (
Select RN  from  (
Select SEQ_VAL, ACC, TERR, SEG2, ORG_ID  ,
    Row_number()OVER(PARTITION BY ACC ORDER BY terr) RN from  @Table1)T WHERE RN = 1 
ORDER BY ACC DESC )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM