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: 1281
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: 1281
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 SQL - select data available in index Nileshkul DB2 3 Mon Jun 26, 2017 1:30 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts RMM: scratching a volume that RMM thi... Hervey Martinez IBM Tools 0 Mon May 15, 2017 8:51 pm
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm


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