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: 154

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
mastrahoyar

New User


Joined: 03 Mar 2007
Posts: 8
Location: hyd

PostPosted: Wed Aug 06, 2008 10:53 am    Post subject: how to delete duplicate rows
Reply with quote

Hi abhishek.

If table contains data like

CUST_ID NAME DEPT

123 ABC A1
123 EFG 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
123 EFG A1
124 ABC A1
125 ABC A1
126 ABC A1

Now can you please tell me the query which delete duplicate rows..
Back to top
View user's profile Send private message
samuel_Inba

New User


Joined: 03 Jan 2008
Posts: 40
Location: Chennai

PostPosted: Wed Aug 06, 2008 11:54 am    Post subject:
Reply with quote

hi,
I have a doubt pls clarify me,


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

The inner query will return all the CUST_ID which has rows more than one,
and the outer query will delete all the rows which was returned by the inner query.
but as per the above requirement,

One occurance should be kept... not all of them to be deleted.
e.g:
1
1
1
2
2
then the output shud be
1
2
...

pls let me know if i understood incorrectly.
Back to top
View user's profile Send private message
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