MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DELETE multiple records from a table

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
batu544
Beginner


Joined: 02 Aug 2005
Posts: 75
Topics: 27

PostPosted: Fri Oct 15, 2010 3:40 pm    Post subject: DELETE multiple records from a table Reply with quote

Hi,
I was trying to delete some records from a table, but not able to do it.. Could anyone please suggest the best way to delete the records from DB2 table as per the below scenario..


I have a table called table1 having following rows...


Table 1
Code:

col1   col2      col3      col4   col5   col6   col7   col8   timestamp
----   ----      ----      ----   ----   ----   ----   ----   ---------
0755   2010-10-10   10:10:20   1   1234   batu   0755   1600   2010-10-10.09.00.00000
0755   2010-10-10   10:10:20   2   1234   batu   0755   1600   2010-10-10.09.00.00000
0755   2010-10-10   10:10:20   3   1234   batu   0755   1600   2010-10-10.09.00.00000
0888   2010-10-11   10:10:20   1   2345   bbbb   0888   1600   2010-10-11.09.00.00000
0888   2010-10-11   10:10:20   2   2345   bbbb   0888   1600   2010-10-11.09.00.00000
0888   2010-10-12   10:10:20   1   2233   bats   0777   1600   2010-10-12.09.00.00000
0888   2010-10-12   10:10:20   2   2233   bats   0777   1600   2010-10-12.09.00.00000





If we will exclude col4 from this table then we can see there are duplicate records in this table. From this duplicate records, only the latest record needs to be delted.

Here the following lines will be considered as the duplicates and needs to be removed..
Code:

0755   2010-10-10   10:10:20   3   1234   batu   0755   1600   2010-10-10.09.00.00000
0888   2010-10-11   10:10:20   2   2345   bbbb   0888   1600   2010-10-11.09.00.00000
0888   2010-10-12   10:10:20   2   2233   bats   0777   1600   2010-10-12.09.00.00000


Now the question is .. Is it possible to delete these duplicate records by writing only queries. ?

It might be helpful to know the primary keys on this table. Primary key is the combination of col1,col2,col3 and col4.

If I can save the only duplicate rows in a new temp table, then is it possible to delete these rows from table1 by writing a single query ?

Any help will be appreciated ..
_________________
Regards,
batu
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Fri Oct 15, 2010 3:54 pm    Post subject: Reply with quote

Please review your "duplicates". I believe the set of posted rows is incorrect.

Or i misunderstand the "rules". . . Confused
_________________
All the best,

di
Back to top
View user's profile Send private message
batu544
Beginner


Joined: 02 Aug 2005
Posts: 75
Topics: 27

PostPosted: Fri Oct 15, 2010 4:11 pm    Post subject: Reply with quote

Hi Papadi -
If you will exclude col4 from the table then you can see there are 3 set of duplicate records. The latest record ( based on the col4's sequence ) needs to be deleted..

Thank you !!
_________________
Regards,
batu
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Oct 15, 2010 4:33 pm    Post subject: Reply with quote

batu544,

Unless I am missing something , isn't it a simple case of finding the max of col4 and deleting it ?

ex : untested sql

Code:

DELETE                                                     
  FROM table                                             
 WHERE (COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9) IN     
       (SELECT COL1,COL2,COL3                               
              ,MAX(COL4)                                     
              ,COL5,COL6,COL7,COL8,COL9                     
          FROM table                                     
         GROUP BY COL1                                       
                 ,COL2                                       
                 ,COL3                                       
                 ,COL5                                       
                 ,COL6                                       
                 ,COL7                                       
                 ,COL8                                       
                 ,COL9);


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
batu544
Beginner


Joined: 02 Aug 2005
Posts: 75
Topics: 27

PostPosted: Fri Oct 15, 2010 5:21 pm    Post subject: Reply with quote

Hi Kolusu,
I am sorry for not including a not duplicate condition in my example.. I can't use the MAX of col4 always.. because there could be a rows for which no duplicates available.. if I will use directly the max function then it will delete the non-duplicate record also..

Also, I fear to put all the columns in where IN condition because in my table there are 54 columns and when I am putting all the columns in 'IN' condition its really taking much time and much relative cost in my test region. So, it will definitely take much more in production region..

Is there any better option.. ?

Thank you..
_________________
Regards,
batu
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Oct 15, 2010 5:34 pm    Post subject: Reply with quote

Quote:
I am sorry for not including a not duplicate condition in my example.. I can't use the MAX of col4 always.. because there could be a rows for which no duplicates available.. if I will use directly the max function then it will delete the non-duplicate record also..


batu544,

You can avoid the deletion of non duplicate rows by adding another condition on the inner sql by like shown below

Code:

         GROUP BY COL1                                       
                 ,COL2                                       
                 ,COL3                                       
                 ,COL5                                       
                 ,COL6                                       
                 ,COL7                                       
                 ,COL8                                       
                 ,COL9 HAVING COUNT(*) > 1 );



This will skip the deletion of all the unique rows.

Quote:

Also, I fear to put all the columns in where IN condition because in my table there are 54 columns and when I am putting all the columns in 'IN' condition its really taking much time and much relative cost in my test region. So, it will definitely take much more in production region..


You want to compare the entire 54 columns for duplicates? bonk It is better to unload the table and run it thru a sort and delete the max (col4) records and reload it back


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Mon Oct 18, 2010 12:42 pm    Post subject: Reply with quote

Quote:

Primary key is the combination of col1,col2,col3 and col4.


If col5 thru col54 is same for a given combination of col1 thru col3, as appears in your sample, then you may skip col5 thru col54 in kolusu's query.
_________________
Regards,
Diba
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group