IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

delete duplicate records using a subquery in from clause


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Thu Nov 25, 2010 2:13 pm
Reply with quote

Hi,

I have a table t1 with col A int and B int.

A B
1 2
2 3
1 2

In the table we can see duplicate records in Table t1.

I am writing a query as below

delete from ( select a,b,count(*) from t1 group by a,b having count(*) >1).

the subquery runs fine if i run it separately. But it give me an error when used as a subquery.

Please help me to solve this issue.

Regards,
L.Nethaji
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Nov 25, 2010 3:05 pm
Reply with quote

Your subselect creates a "work table" , you can't delete from a work_table.

you 'll need something like
delete from T1 where ...
and if you delete all rows having count(*) > 1 you will be deleting both rows.

If you want to delete with a "Where..." you'll need something that uniquely defines the row you want to delete, like timestamp.
There are other ways like unloading (without duplicates)/reloading or copying (without duplicates) to a private table /empty the original/copy back.
or a program with a cursor and a delete where current of if equal to the previous fetch.
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Thu Nov 25, 2010 3:42 pm
Reply with quote

Hi Guyc,

Thanks for the reply ,

I hope work table u mentioned is a table temporarily created .

I tried the below query for the same table t1

" Delete from (select b from test.t1 where a=1) "

DB2 creates a temporary or work table for the subquery but it rightly deleted the records having a = 1 .

Please let me know what the process wrongly happening behind when we use group by and count(*).

I have tried it and its working fine if we use current of using cursor.

I am trying to delete duplicates using subquery without using cursors.

Thanks in advance,
L.Nethaji.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Nov 25, 2010 3:49 pm
Reply with quote

the rules for a subquery are like the rules for a view :
some are updateable, some are not.
subselects with group by are not updateable.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Nov 25, 2010 4:12 pm
Reply with quote

there is no subselect that will allow you to delete only the duplicate , if you don't have any other column(-combination) to uniquely define the row
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Thu Nov 25, 2010 5:28 pm
Reply with quote

Hi,

Thanks u Guyc ,

Cheers,
Nethaji.L
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 2
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts DELETE SPUFI DB2 1
No new posts Duplicate transid's declared using CEDA CICS 3
No new posts DSNTIAUL driven delete IBM Tools 0
Search our Forums:

Back to Top