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
 
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 Compare yesterday's date to the one o... migusd SYNCSORT 11 Fri Sep 22, 2017 11:35 pm
No new posts Validate the Date girishb2 DFSORT/ICETOOL 9 Tue Sep 19, 2017 1:12 am
No new posts column with count of rows within dist... ronald wouterson DB2 4 Sun Sep 17, 2017 9:48 pm
No new posts Convert rows and column into JSON for... Dinesh Manivannan DB2 2 Sun Sep 03, 2017 6:50 pm
No new posts Julian Date to CICS ABSTTIME blayek CICS 3 Wed Aug 30, 2017 11:15 pm

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