Delete the duplicate and keep the max records.....

I would like to remove the duplicate records based on columns ID and VAL but keep the max SAL records. ID + VAL is the key in the table and just delete same records by keeping the max sal.
Note: Eventhough there are two records for the same max SAL, just keep one
eg
SQL>  select * from temp_fa;
        ID        VAL        SAL
         1        100         10
         1        100         20
         1        100         20
         2        200         10
         3        300         10
         3        300         30
         4        400         10
         4        400         10
         5        500         10
         5        500         20
         5        500         20
After deleting the table should looks like
SQL>  select * from temp_fa;
        ID        VAL        SAL
         1        100         20
         2        200         10
         3        300         30
         4        400         10
         5        500         20

user520824 wrote:
I would like to remove the duplicate records based on columns ID and VAL but keep the max SAL records. ID + VAL is the key in the table and just delete same records by keeping the max sal.
Note: Eventhough there are two records for the same max SAL, just keep one
eg
SQL>  select * from temp_fa;
ID        VAL        SAL
1        100         10
1        100         20
1        100         20
2        200         10
3        300         10
3        300         30
4        400         10
4        400         10
5        500         10
5        500         20
5        500         20
After deleting the table should looks like
SQL>  select * from temp_fa;
ID        VAL        SAL
1        100         20
2        200         10
3        300         30
4        400         10
5        500         20
Hi,
In this script I included sal in the key because it's more safe for you.
        ID        VAL        SAL
         1        100         10
         1        100         20
         1        100         20
         2        200         10
         3        300         10
         3        300         30
         4        400         10
         4        400         10
         5        500         10
         5        500         20
         5        500         20
--1.Preserve first row from all duplicates
create table first_duplicate as
(select distinct val, id, sal
from temp_fa
where to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) in
(select to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) p_key
      from temp_fa
      group by to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal)
         having count(to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal))>1)
--2.Delete all duplicates
DELETE  FROM temp_fa
where to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) in 
     (select to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) p_key
     from temp_fa
     where to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) in
          (select to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) p_key
          from temp_fa
           group by to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal)
              having count(to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal))>1)
--3.Add first row from all duplicates
insert into  temp_fa (val, id, sal)
select * from first_duplicate;
--4.Delete rows that don't have the max salary
DELETE  FROM temp_fa
where to_char(temp_fa.id)||to_char(temp_fa.val)||to_char(temp_fa.sal) in 
     (select to_char(id)||to_char(val)||to_char(sal) p_key
     from temp_fa
          MINUS
     (select to_char(x.id)||to_char(x.val)||to_char(x.sal) p_key
     from temp_fa x,
     (select val, id, max(sal) max_val from temp_fa
       group by  val, id ) y
       WHERE x.val = y.val and
        x.id  = y.id and
        x.sal =max_val
HR: XE > select * from temp_fa order by id;
        ID        VAL        SAL
         1        100         20
         2        200         10
         3        300         30
         4        400         10
         5        500         20
HR: XE > Regards,
Ion
Edited by: user111444777 on Sep 25, 2009 10:42 PM

Similar Messages

  • Delete the parent records and child table records at a time

    hi all;
    I am facing the pbm like to delete the all records in child table and corresponding records in parent table at a time. so I want to delete the all the records in child table and corresponding parent records in parent table by using single SQL query. plz help me
    Thanks in advance

    You want to use one single SQL statement to delete the child records in a table and the corresponding master records in the master table??
    That's not quite possible with a single SQL, of course unless you are talking about Oracle Forms, where you have a relation and set the delete behavior to Cascading, like said in the above posts.
    Tony

  • Each song on my iTunes has a duplicate. How do I delete all duplicates and prevent this from happening again in the future?

    Each song on my iTunes has a duplicate. How do I delete all duplicates and prevent this from happening again in the future?

    After I had updated to Itunes 11 (also on Windows 7), I had the same experience, but I suspect that it was because I accepted a proposal to set up an external media library in order to simplify security backups. An associated result was that I had lost all of my playlists!
    What I did was to restore the entire library off my Ipod, using a very good program called PodToPC (a free download is available) selecting an option to replace all of the stuff in the Itunes library. I use the Ipod Classic simply because it has a large capacity to hold my collection of CD and Vinyls. No way was I going to modify duplicates of 10000 tracks in 750 playslists by hand!  I did have a few residual problems with some of the playlist specs but, after a few hours of work all was restored.
    I hope this is useful.

  • Large number of duplicate listings of songs. 1 (or several) is found and then 1 or more with the circled "!" in col 1. I would like to automatically delete all duplicates and then all marked as not found.

    large number of duplicate listings of songs. 1 (or several) is found and then 1 or more not-found with the circled "!" in col 1. I would like to automatically delete all duplicates and then all marked as not found. How do I do this without losing my one good copy?

    large number of duplicate listings of songs. 1 (or several) is found and then 1 or more not-found with the circled "!" in col 1. I would like to automatically delete all duplicates and then all marked as not found. How do I do this without losing my one good copy?

  • DELETE THE MATCHED RECORDS IN DB2 TABLE

    DELETE THE MATCHED RECORDS IN DB2 TABLE
    sql server table sqlserver_emp(c1,c2,c3,4)
    records:1 2 3 4
    DB2 table db2_emp(c1 key,c2,c3,c4)
    records:1 2 5 6 7 8
    Both tables having same structure
    Matched records : 1 2
    1.Delete the matched records in db2 table : 1 2 (without using truncate option for DB2 in Anywhere)
    2.Finally Load all records in sql server(Because duplicate records in db2 already removed so no duplicates occured)
    3.NEED final OUTPUT AS:db2 table: 5 6 7 8 1 2 3 4
    Note:
    1.DB2 truncate doesn't used
    2.STARING AREA: ORACLE
    3.SQLSERVER AND DB2 CLOSED ENVIRONMENT DOESN'T USED AS STAGING AREA.
    HOW MANY INTERFACES AND PROCEDURES SHOULD BE CREATE? WHAT ARE THEY?
    HOW TO REACH MY REQUIREMNT?
    Answer provider is more appricatable.
    Thanks in advance.
    Edited by: krishna on Nov 9, 2011 8:40 PM

    1st option
    See in this scenario you can use two interface and one procedure step.
    1st interface-->poricedure-->2nd interface
    1st interface will bring data to oracle staging-->then procedure will delete matched record from target using keys--->3rd interface will simple insert the the data in oracle staging.
    2nd option
    Insted of deleting the target matched records, you just update target matched record with latest records in your staging oracle.You can use IKM Merge
    Thanks

  • How to delete the material records related to storage type

    Dear SAP guru's,
    I got a situation that if the material "XXXXXX" exists in two different storage type (let say 001 & 002 ) and the material needs to be removed out of one (Let say 002) to avoid the picking and putaway in future.
    Inorder to accomplish an above requirement we tried the below cases
    Case 1: By flagged the material "XXXXXX" under storage type 002 in MM06 transaction.But it didn't restrict the system on allocating and removing the stock from /to to the storage type 002.
    Case 2:After we flagged the material for deletion under storage type 002 ,we used to MM71 transaction to archive & delete the material record that assocaited with the storage type 002 completely.
    I am kind of queiries to know if there is any other method or procedure available to delete the storage type associated material records in SAP.
    Please guide me with the other options.Thanks.
    Regards,
    John.

    Hi John,
    Its very simple, remove the Storage type indicators from the material master - Warehouse View 1 at Stock placement & Stock removal section, with this it won't pickup or place the materials in the concerned storage type.
    If you have extended this materials to more than 1 storage type, you need to do the same for all the storage types.
    Reward if it is helpful.

  • How to delete the duplicated records, not just surpress the records?

    I am new to CR. Right now I am doing a project which needs CR get query from Oracle. I got the query from Oracle. There are records with duplicated fields. For example (the following only show part of fields):
    ID    body_code  
    1            10            
    2             10            
    3             15            
    4              15           
    5              15           
    6              16           
    I need to only select records (not surpress, because I will do some caluculate later) like following:
    ID         body_code        
    1             10         
    2              15
    3               16   
    I tried to creat selection fomula in fomula workshop, shown as follows:
    onlastrecord;
    <>next
    but CR said next can be evaluated. I think it must have something to do with the print-time. So what show I do to delete the duplicated records.Thank you very much.

    Ting,
    Try this:
    Insert a group on body_code.  Then create a running total called Distinct Count.  Field to summarize -> ID, Evaluate on change of group Group # 1 Body Code, and Never Reset.
    Then insert a chart in the report header:
    In the advanced layout, select body_code on change of and select Distinct Count running total in the Show values.
    I hope I understood what you're looking to accomplish.
    Z

  • How to delete the single record from the  ODS

    HI,
       I want to DELETE  the particular record from ODS.. how to delete that..
    plz give me the solution.. it's urgent..
    with regards
    @jay

    Hi Prakash,
        You can delete a particular record from ODS by specifying the key field name in Selective deletion button available in Contents tab in the manage screen of ODS. But it will only delete the record from the active data table and not from the Change log and new data table . You should be very carefull if you are doing in Production system as there is chance of deleting other records accidentally.
    Go to Selective deletion button - > Click on delete selection -> Enter the key field values of the record you want to delete-> Press save button to save as Variant -> Go back and click on selection button -> Chosse the background job type -> Click on start button.
    Regards,
    Prakash

  • DELETING THE MASTER RECORDS

    Hi gurus,
    i want to upload the new master records into my system regarding the MM. first thing i want to delete the old records in the system and freshly want to load into the system. please give me the soluion how to do it.
    it is a development server..
    thanks in advance..
    ravi

    i want to upload the new master records into my system regarding the MM. first thing i want to delete the old records in the system and freshly want to load into the system. please give me the soluion how to do it.
    >
    > it is a development server..
    When uploading the new master records, just assign the previous numbers used by the old master records and the new upload will overwrite those old records. In this case deleting/overwriting the old records.
    If you dont assign the new upload any numbers, it will append below the exisiting records, that way not deleting the old records.

  • Delete the parent record

    hi all,
    based on the thread i got the list of child tables.
    Re: to find the tables which is having link with particular table.
    no i have doubt in deletion process.
    My ultimate aim is to delete the bl_det records.
    let me explain with ex.
    i have table
    BL_DET --parent table with comp and ref as primary key
    which is having child rec.
    BL_CON --child of BL_DET(comp,ref) primary key comp,ref and serial
    BL_CON_PCK --is child of BL_CON(comp,ref,serial) primary key comp,ref,serial,pck_gp_srl,gp_srl.
    DELETE BL_CON_PCK
    WHERE BCK_COMP = :GLOBAL.comp
    AND BCK_REF = :parameter;
    DELETE FROM BL_CON
    WHERE BCR_COMP = :GLOBAL.comp
    AND BCR_REF = :paramter
    DELETE FROM BL_DET
    WHERE BL_COMP = :GLOBAL.comp
    AND BL_REF - :parameterwill delete the correct set of records.(my doubt area is delete statement written for the table BL_CON_PCK).
    Please tell me if i am wrong.
    Thanks..

    (my doubt area is delete statement written for the table BL_CON_PCK).Cannot understand your doubt. What is your doubt, are you getting any error while executing your code?
    To delete a record from parent table all child references must be deleted. So your delete statement appears right
    -- How delete should progress
    BL_CON_PCK >> BL_CON >> BL_DET
    {code}
    If you have created foreign constraint using ON DELETE CASCADE or ON DELETE SET NULL then you can straight away delete from parent table and all child references are deleted/set as null respectively. Read Oracle Documentation to see details
    Short demonstration
    {code:sql}
    SQL> CREATE TABLE A(a1 NUMBER PRIMARY KEY , a2 NUMBER);
    Table created
    SQL>  CREATE TABLE B(b1 NUMBER PRIMARY KEY , a1 NUMBER);
    Table created
    SQL>  CREATE TABLE C(c1 NUMBER, b1 NUMBER);
    Table created
    SQL>  ALTER TABLE B ADD CONSTRAINT FK_B FOREIGN KEY (a1) REFERENCES A ON DELETE CASCADE;
    Table altered
    SQL>  ALTER TABLE C ADD CONSTRAINT FK_C FOREIGN KEY (b1) REFERENCES B ON DELETE CASCADE;
    Table altered
    SQL>  INSERT INTO A VALUES (1,2);
    1 row inserted
    SQL>  INSERT INTO B VALUES (3,1);
    1 row inserted
    SQL>  INSERT INTO C VALUES (4,3);
    1 row inserted
    SQL>  SELECT * FROM A;
            A1         A2
             1          2
    SQL>  SELECT * FROM B;
            B1         A1
             3          1
    SQL>  SELECT * FROM C;
            C1         B1
             4          3
    SQL>  DELETE FROM A WHERE A1=1;
    1 row deleted
    SQL>  SELECT * FROM A;
            A1         A2
    SQL>  SELECT * FROM B;
            B1         A1
    SQL>  SELECT * FROM C;
            C1         B1
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to delete the matching records from two internal tables

    Hi ,
    I have two internal tables say A and B of the same type. If A has 10 records and B has 4 records , I want to delete the 4 records in B from A .
    loop at B into wa .
    delete A where key = wa - key .
    endloop.
    takes a long time if the table B is huge. how can I improve the performance.
    Thanks.
    Gayathri

    Hi Gayathri,
    You could try field-symbols. It reduces the data transfer from the internal table B to the work area.
    field-symbols <fs_itab_b> like line of B.
    loop at B assigning <fs_itab_b>.
      delete A where key = <fs_itab_b>?-key.
    endloop.
    Regards,
    <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=zwcc%2fwm4ups%3d">anand Mandalika</a>.

  • Select statement from a join file deletes the primary records with MS Expl.

    Hello,
    is reality,
    Select statement from a join file deletes the primary records with MS Explorer 6, Firefox not, this with a normal data provider or with a normal "select..." statement.
    This is very strange, I should have a.a.s.p. a solution or workaround
    Thanks, Franco.
    Message was edited by:
    fbiaggi

    Please see the following excerpt from the online documentation.
    http://docs.oracle.com/cd/E11882_01/server.112/e22490/ldr_modes.htm#SUTIL1332
    Enabled Constraints
    During a direct path load, the constraints that remain enabled are as follows:
    NOT NULL
    UNIQUE
    PRIMARY KEY (unique-constraints on not-null columns)
    NOT NULL constraints are checked at column array build time. Any row that violates the NOT NULL constraint is rejected.
    Even though UNIQUE constraints remain enabled during direct path loads, any rows that violate those constraints are loaded anyway (this is different than in conventional path in which such rows would be rejected). When indexes are rebuilt at the end of the direct path load, UNIQUE constraints are verified and if a violation is detected, then the index will be left in an Index Unusable state. See "Indexes Left in an Unusable State".

  • Form wont delete the last record

    Hi All
    I have the follwoing Form
    e.g
    First_name || Last_name || Emp_id
    The Forms works ok when populating the form and saves,you can delete all the records except for the last record if you delete the last record it's save.
    Does anyone whan what could be the problem.
    please help i'm new to Forms.

    you can delete all the records except for the last record if you delete the last record it's saveSo, can or cannot you delete the last record?
    What do you mean with "it´s save"?

  • Attribute change run fails - "Error when deleting the data record /B05/X.."

    Hello,
    I get the error message "Error when deleting the data record /B05/X.." on doing the change run.
    I have already done RSRV on this objects and found no errors, but still the activation fails.
    please give your suggestions ASAP to fix the issue as it is on production system.
    Thanks.

    Attribute change run could fails due to 2 reasons-
    (1) attribute xchange run is already runnnig for any toher object(monitor lock) (check by thi prog-RSDDS_CHANGERUN_MONITOR)
    (2) data is loading deleting from the object on which attribut changerun is running.(go in a manage tab and check)
    check this two conditions and rerun the attribute changrun again from program - RSDDS_AGGREGATES_MAINTAIN and give the master data name in the selection.
    Hope it helps you.

  • Does anyone know the max record time in voice memo?

    Hi I am trying to find out what the max record time is for 1 voice memo on an i-phone 4 with 32 gb of ram.
    I can not find it documented any where.
    Thanks
    Mark

    Thanks Ingo2711,
    The pages you suggested point to the local carriers as the only authorized service centers.
    The problem with Etisalat is that they outsourced that service to another company called Technocare.
    That company sent the phone back twice (for the reasons mentioned above) and suggested that I have it fixed at a third party, which I am refusing to do since that will void my warranty. Additionally, they are refusing to give me any thing in writing to that affect.

  • How to Delete the condition record in CRM

    HI,
    Can you please help me how to delete the condition record from condition table in CRM.
    Please explain the usage of FM CRMXIF_CONDITION_SEL_DELETE with examples.
    I have also read the documention of the function module. How to use this FM for custom defined condition table.
    (this is the code given in Documentation)
    DATA-OBJECT_REPRESENTATION         = 'E'
    DATA-SEL_OPT-CT_APPLICATION              = 'CRM'
    DATA-SEL_OPT-OBJECT_TASK                    = 'D'
    DATA-SEL_OPT-RANGE-FIELDNAME        = 'PRODUCT_ID'
    DATA-SEL_OPT-RANGE-R_SIGN                  = 'I'    (Including)
    DATA-SEL_OPT-RANGE-R_OPTION           = 'EQ'
    DATA-SEL_OPT-RANGE-R_VALUE_LOW  = 'PROD_1'
    Thanks
    Shankar

    Hi Shankar,
    I am using the same CRMXIF_CONDITION_SEL_DELETE function module to delete condition record present in CRM.
    But it is giving me below error in the return table of the FM after i run the program. Can you please correct me if I am doing any thing wrong?
    Error in  lt_return: SMW3     CND_MAST_SEL_DEL_EXT_VALIDATE     CND_M_SD
    code:
    ls_range-fieldname = 'PRODUCT_ID''.
    ls_range-R_SIGN = 'I'.
    ls_range-R_OPTION = 'EQ'.
    ls_range-R_VALUE_LOW = '123456'.
    APPEND ls_range TO lt_range.
    MOVE lt_range TO ls_entry-SEL_OPT-range.
    ls_data-SEL_OPT-object_task = 'D'.
    ls_data-SEL_OPT-ct_application = 'CRM'.
    ls_data-object_representation = 'E'.
    CALL FUNCTION 'CRMXIF_CONDITION_SEL_DELETE'
      EXPORTING
        DATA          = ls_date
    IMPORTING
       RETURN        = lt_return
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      IMPORTING
        return = lt_ret.
    Edited by: Saravanaprasad Nadar on Jul 7, 2010 1:27 AM

Maybe you are looking for

  • Maximum Bitrate/Sample Rate?

    What is the maximum bitrate and sample rate(44.1/48k) that can be read by my iPod? Does it depend on whether I use mp3 or aac encoding? Can I use variable bit rate (VBR)? If I can use a 48k sample rate, would that mean using a smaller bitrate?

  • How to debug ACE FT Sync Problems ?

    Hello, in one of our contexts we have a sync problem on the standby unit. "sh ft group detail" gives "Running cfg sync status : Error on Standby device when applying configuration file replicated from active", while "Startup cfg sync status" is OK. "

  • New line charecter in a file

    Hi all, Can anybody please tell me how to put a new-line charcter in a txt file opened in Binary mode for O/p. I cant use text mode or multitple transfers for other reasons in my scenario. I tried using hex field of value 0A and 10, didnt work out. A

  • SunCluster 3.2 - Error when accessing the SC management console

    Hi, I have a SC 3.2 with two nodes. I defined a VIP for the both nodes. When accessing to the Web GUI on the VIP, I cannot access to the Systeme Application. When the 1st node is online, the Systeme management is accessible. When switching to the oth

  • Installing VS Ultimate and Community on the same computer

    I use VS Ultimate for work and would like to use VS Community for my own side projects.  If I install community first, Ultimate seems to overwrite it.  If I install Ultimate first, the Community installer says to first uninstall Ultimate.