Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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 HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Repeat part of record multiple times Learncoholic DFSORT/ICETOOL 4 Tue Aug 29, 2017 11:33 am
No new posts Join records from 2 files with No Dup... Poha Eater DFSORT/ICETOOL 22 Sun Aug 27, 2017 10:35 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us