Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
How 2 delete dup rows with single qury in DB2?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions
Author Message
die7nadal

Active User


Joined: 23 Mar 2005
Posts: 142

PostPosted: Fri Jan 04, 2008 4:51 am    Post subject: Re: Reply to: How 2 delete dup rows with single qury in DB2?
Reply with quote

abhishekmdwivedi wrote:
Quote:

Hi correcting my previous question..

Can any one has idea about how to delete all duplicate rows from table with single query?

e.g. table contains data like

CUST_ID NAME DEPT

123 ABC A1
123 ABC A1
123 ABC A1
124 ABC A1
125 ABC A1
126 ABC A1
126 ABC A1

Output should be

CUST_ID NAME DEPT
123 ABC A1
124 ABC A1
125 ABC A1
126 ABC A1


As per the above requirement , below query may do the trick.

DELETE FROM table_name where
CUST_ID in
(SELECT CUST_ID from table_name
GROUP BY CUST_ID HAVING COUNT(*) > 1) ;


This query will delete all the dups, but the requirement was to keep 1 record among the duplicate records. So ineffect the below records will not be present in the output.

123 ABC A1
126 ABC A1
Back to top
View user's profile Send private message
References
PostPosted: Fri Jan 04, 2008 4:51 am    Post subject: Re: Re: Reply to: How 2 delete dup rows with single qury in DB2? Reply with quote

Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions All times are GMT + 6 HoursGoto page Previous  1, 2
Page 2 of 2