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

Need query to select duplicates which is part of primary key


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

New User


Joined: 27 Dec 2007
Posts: 73
Location: India

PostPosted: Mon Mar 01, 2010 6:20 pm
Reply with quote

Hi,
Lets assume that a table has below fields and all of them part of primary key. For e.g.. The structure of table like below.

empseq empname empgrade
1 aaa Y
1 bbb Y
2 aaa Y
1 aaa z

I want to select those records from this table with duplicate emp names but unique empseq and empgrade combination. Now the
result of my query should return below,

1 aaa Y
2 aaa Y

Can the query be written without any joins on the same table?

Please let me know if I am not clear enough..Thanks!
Back to top
View user's profile Send private message
knn9413

New User


Joined: 23 Jul 2009
Posts: 17
Location: US

PostPosted: Tue Mar 02, 2010 2:01 am
Reply with quote

do a group by on those columns with a having clause > 1... that should work...
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Mar 02, 2010 2:50 am
Reply with quote

Hello,

Quote:
Can the query be written without any joins on the same table?

Quote:
do a group by on those columns with a having clause > 1...

To do this, i suspect there will be a self-join?
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Mar 02, 2010 10:55 pm
Reply with quote

You could use "where exists".

but I am not sure of your requirements :
why would it return
1 aaa Y
2 aaa Y
and not return
1 aaa Z
?
Back to top
View user's profile Send private message
technut

New User


Joined: 27 Dec 2007
Posts: 73
Location: India

PostPosted: Wed Mar 03, 2010 10:23 am
Reply with quote

Apologize!!! I framed the it a little wrong. The sequence number will get incremented only when both name and grade are same. Hence I wanted those dupliacte employee names with the same grade but unique Grade and Req combination.

If possible, can you explain on the solution employing where exists? Thanks..
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Mar 04, 2010 1:59 pm
Reply with quote

Code:
select * from tab1 A
where exists(select * from tab1 B
 where A.grade = B.grade
   and A.name  = B.name
   and A.seq  <> B.seq)
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts SELECT from data change table DB2 5
Search our Forums:

Back to Top