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
 

 

Select Max date column

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Select Max date column
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    Post subject:
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    Post subject:
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    Post subject:
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: 1013
Location: India

PostPosted: Wed Oct 24, 2012 1:19 pm    Post subject:
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

Site Director


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

PostPosted: Wed Oct 24, 2012 6:49 pm    Post subject:
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    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 DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts TOT & DATE parameter in ICEMAN hemanthj642 DFSORT/ICETOOL 4 Mon Nov 14, 2016 5:19 am
No new posts SQZ pipe delimited file with 1 column... Rick Silvers DFSORT/ICETOOL 2 Sun Nov 06, 2016 8:11 pm
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
This topic is locked: you cannot edit posts or make replies. How to pass the previous month date i... Suganya87 DFSORT/ICETOOL 5 Mon Oct 31, 2016 4:13 pm


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