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 Loading data to table gives wrong for... Raghu navaikulam DB2 18 Thu Jul 13, 2017 2:11 pm
No new posts How to add header with Date(YYMMDD) i... Rajan Moorthy DFSORT/ICETOOL 2 Thu Jul 06, 2017 11:44 pm
No new posts SQL - select data available in index Nileshkul DB2 3 Mon Jun 26, 2017 1:30 am
No new posts Validate date and numeric fields and ... Rick Silvers DFSORT/ICETOOL 6 Thu May 11, 2017 6:51 pm
No new posts LISTIDR compiled date/time jerryte IBM Tools 3 Thu Apr 20, 2017 7:37 pm


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