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

Select Max date column


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

New User


Joined: 21 Dec 2007
Posts: 88
Location: My Desk

PostPosted: Tue Oct 16, 2012 1:00 am
Reply with quote

Hi,

Code:
C1 C2
2  12/31/1980
2  12/31/1983
2  12/31/1985
3  12/31/1986
3  04/30/1987
4  12/31/1987
4  12/31/1988
4  07/31/1990
5  04/30/1996
5  06/30/1997
6  12/31/1997
6  04/30/1998


I need to select distinct Column 1 and the max date for each column 1 value as below,

Code:
2  12/31/1985
3  04/30/1987
4  07/31/1990
5  06/30/1997
6  04/30/1998


Please assist me with the query.

Thanks
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Tue Oct 16, 2012 1:32 am
Reply with quote

How about showing us what you have tried and the results?
Back to top
View user's profile Send private message
PrabakarV

New User


Joined: 21 Dec 2007
Posts: 88
Location: My Desk

PostPosted: Tue Oct 16, 2012 1:35 am
Reply with quote

Code:
SELECT C1, MAX(C2)
FROM TABLE                         
GROUP BY C1
WITH UR;         


Got it. Thanks for the help.
Back to top
View user's profile Send private message
dejunzhu

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Wed Oct 24, 2012 6:52 am
Reply with quote

what if I want to retrieve all the rows except the max date???
I want to get below result, how to compose the SQL statement?
Code:
2  12/31/1980
2  12/31/1983
3  12/31/1986
4  12/31/1987
4  12/31/1988 
5  04/30/1996 
6  12/31/1997


I know we can use 'IN' or 'NOT EXIST' keyword, but because of SQL performance issue of the two keywords, some other better choice is wanted.
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Wed Oct 24, 2012 1:19 pm
Reply with quote

Hi Dejunzhu,

OLAP Functions can be used, but icon_neutral.gif still there would be performance issues, because db2 would have read the data first and then order it.

not tested
Code:
select c1,c2
from
(select c1,c2, desnse_rank() over(partition by c1,c2
              order by c1) as RANK) AS table
where rank >= 2

May be you can try it with more data and let us know which method is best/faster.

Thanks,
Sushanth
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: Wed Oct 24, 2012 6:49 pm
Reply with quote

Hello,

You might try 2 queiies. One to find the max and the other to select everything less than the max. It might make a big difference if this date is an index.
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 Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts How to load to DB2 with column level ... DB2 6
No new posts RC query -Time column CA Products 3
No new posts Need to convert date format DFSORT/ICETOOL 20
Search our Forums:

Back to Top