Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Need query to select duplicates which is part of primary key

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Need query to select duplicates which is part of primary key
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    Post subject:
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

Site Director


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

PostPosted: Tue Mar 02, 2010 2:50 am    Post subject:
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: 1278
Location: Belgium

PostPosted: Tue Mar 02, 2010 10:55 pm    Post subject:
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    Post subject: Reply to: Need query to select duplicates which is part of p
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: 1278
Location: Belgium

PostPosted: Thu Mar 04, 2010 1:59 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
No new posts Removing Duplicates based on certain ... chandracdac DFSORT/ICETOOL 8 Fri Dec 09, 2016 4:40 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us