MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Eliminating Duplicates - DB2 Query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
sakreg
Beginner


Joined: 28 Feb 2005
Posts: 80
Topics: 26

PostPosted: Wed Jun 29, 2005 4:06 am    Post subject: Eliminating Duplicates - DB2 Query Reply with quote

Hi

I have data in a Table as Below:
Quote:

Number Ver Code Eff Dt
*****************************
0710001 00 00008 2005-04-12
0710001 00 00015 2005-04-12
0710001 00 00017 2005-04-12
0710001 00 00028 2005-04-11
0710001 00 00028 2005-04-12
0710001 00 00112 2005-04-12
0710001 01 00008 2005-04-12
0710001 01 00015 2005-04-12
0710001 01 00017 2005-04-12
0710001 01 00028 2005-04-12
0710001 01 00112 2005-04-12
0710001 01 00112 2005-03-12
*****************************

I want to eliminate some rows using SQL Query based on the rule:

Rule: For a given Number and a Version, if there are duplicate Code, I need to pick-up only one based on the highest Eff Date.

Based on the data I have shown, I need to eliminate the one I have shown in Red and select the rest using a SQL Query.In Select Clause I need all the columns.

Any Suggestions, please let me know
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Wed Jun 29, 2005 5:35 am    Post subject: Reply with quote

sakreg,


Try this . I haven't tested it out

Code:

SELECT *
  FROM TABLE
 WHERE (Number,Ver,Eff_Dt) IN (SELECT Number
                                     ,Ver
                                     ,MAX(Eff_Dt)
                                  FROM TABLE
                                 GROUP BY NUMBER
                                         ,VER)
 ;


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sakreg
Beginner


Joined: 28 Feb 2005
Posts: 80
Topics: 26

PostPosted: Thu Jun 30, 2005 1:08 am    Post subject: Reply with quote

Thanks for your reply kolusu.

I got a SQL error code of -120.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jun 30, 2005 4:37 am    Post subject: Reply with quote

sakreg,

Did you run the exact sql ? The query ran fine for me and provided me with the results. Any way here is an explanation of sql code -120

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNMCH11/2.1.6.25?SHELF=&DT=20010718150520&CASE=

kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
abalan76
Beginner


Joined: 01 Jul 2005
Posts: 1
Topics: 0

PostPosted: Fri Jul 01, 2005 4:59 am    Post subject: Reply with quote

Sakreg,
Can you try this
First post , haven't tested it
Code:

SELECT * FROM TABLE A                   
WHERE EFF_DT = (SELECT MAX(EFF_DT)                 
                 FROM TABLE B           
                 WHERE A.NUMBER = B.NUMBER   
                 AND A.VER = B.VER)
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Fri Jul 01, 2005 5:17 am    Post subject: Reply with quote

abalan76,

You need an IN statement in your WHERE clause of your query as the subquery will result in a multiple rows.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sakreg
Beginner


Joined: 28 Feb 2005
Posts: 80
Topics: 26

PostPosted: Mon Jul 04, 2005 3:45 am    Post subject: Reply with quote

abalan76, Thanks for your time.

kolusu, I was wrong when I tried that before. Sorry for that. The query works like a charm. Thanks for that.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group