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

How To Retrieve only dulicates in my table?


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

New User


Joined: 11 Apr 2006
Posts: 93

PostPosted: Fri May 19, 2006 3:38 pm
Reply with quote

I have a DB2 table that contain duplicates, Now I want to retrieve only duplicates, How can i get, anyone please tell me answr for this question?
Back to top
View user's profile Send private message
rajandhla

Active User


Joined: 18 Oct 2005
Posts: 182
Location: Luton UK

PostPosted: Fri May 19, 2006 3:59 pm
Reply with quote

use group by <on key> and having count(*)>1
Back to top
View user's profile Send private message
babu_hi

New User


Joined: 11 Apr 2006
Posts: 93

PostPosted: Fri May 19, 2006 4:16 pm
Reply with quote

Can u please explain clerly?
Back to top
View user's profile Send private message
Gurmeet

New User


Joined: 22 Feb 2006
Posts: 46
Location: Pune

PostPosted: Fri May 19, 2006 5:31 pm
Reply with quote

I think what rajandhla means is:

user group by on the primary key of the table where count is greater than 1... good idea...

i m trying it out but not able to get the query right...

rajandhla,

Can you please give a sample query...

Regards,
Gurmeet
Back to top
View user's profile Send private message
rajandhla

Active User


Joined: 18 Oct 2005
Posts: 182
Location: Luton UK

PostPosted: Fri May 19, 2006 6:05 pm
Reply with quote

Babu,

Please find the sample query

SELECT BKG_REF, count(*) as Total_rows
FROM OSCUAT.TOSBKNG
GROUP BY BKG_REF
HAVING COUNT(*)>1;

This query first groups all the rows based on the given key value ( i.e here bkg_ref is the key )then it selects the group of rows which has got more than two 1 row(simillar)

Hope this clarifies....

Regards
jai
Back to top
View user's profile Send private message
die7nadal

Active User


Joined: 23 Mar 2005
Posts: 156

PostPosted: Fri May 19, 2006 6:05 pm
Reply with quote

Gurmeet,
If you use GROUP BY on ur PRIMARY key having COUNT > 1, you will not be returning any rows. Since Primary Key cannot have any dups.

Code:

SELECT C1, COUNT(*)
GROUP BY C1
HAVING COUNT(*)>1
WITH UR;


Where C1 can be one or more Columns, on which you are trying to find the dups.
Back to top
View user's profile Send private message
Gurmeet

New User


Joined: 22 Feb 2006
Posts: 46
Location: Pune

PostPosted: Fri May 19, 2006 6:36 pm
Reply with quote

die7nadal,

Oopss, i got it wrong...

actually there are certain cases on our development environment where in we have duplicates(primary key), though its theoritically incorrect.

Gurmeet
Back to top
View user's profile Send private message
Gurmeet

New User


Joined: 22 Feb 2006
Posts: 46
Location: Pune

PostPosted: Fri May 19, 2006 6:38 pm
Reply with quote

die7nadal,

Oopss, i got it wrong...

actually there are certain cases on our development environment where in we have duplicates(primary key), though its theoritically incorrect.

Thanks for your help,i got the correct query now, it worked icon_smile.gif
Gurmeet
Back to top
View user's profile Send private message
die7nadal

Active User


Joined: 23 Mar 2005
Posts: 156

PostPosted: Sat May 20, 2006 8:52 pm
Reply with quote

Just curious.

What did u mean by a primary key, a P Key must have a unique index, in which case it cannot have dups. If it has dups, then it shud have duplicate Index defined on it, in that case it is a Duplicate Key.
Back to top
View user's profile Send private message
Gurmeet

New User


Joined: 22 Feb 2006
Posts: 46
Location: Pune

PostPosted: Mon May 22, 2006 5:43 pm
Reply with quote

Yes you are right... but in our development environment some of the DBs are in a terrible state. We have unique primary indexes defined on the table but then also found duplicates, really not sure how the earlier DBAs managed to do this....
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 Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top