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

Need help in query , finding max values


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

New User


Joined: 06 May 2008
Posts: 33
Location: Chennai

PostPosted: Wed Jun 17, 2009 11:57 am
Reply with quote

Hello Everyone!,

I need help in framing one query:
Details:
Below are 3 fields and I want to group rows ,condition as follows.

Show row for latest date (for field B )
by latest date I mean 06/28/2002 >01/14/2001 , 06/28/2002 is latest date.
If
there are two latest date
then
take field C and find out latest date rows in C

See below for more explanation.

Table(TB ) has following fields( just giving fields in question):
Code:

A        B                       C
2       06/28/2002         01/14/2001
2       11/21/1997         04/25/1995
2       09/19/2006         12/27/2002  <--- B has latest date ,so we show this row

3       01/23/2001         11/22/1999
3       02/19/2007         10/23/2002 <---  B has latest date , so show this row
3       17/06/2000         09/14/2002 

4       10/25/2002         06/22/2001
4       10/25/2002         04/24/1980 

<--- B has latest date but  10/25/2002 appears twice ,now consider C and  06/22/2001 > 04/24/2001, so show this row.

4       04/25/2002         05/12/1981
4       03/13/1999         02/11/1985

5       06/06/1998         05/05/1995

The output should be :

A       B                      C
2       09/19/2006         12/27/2002
3       02/19/2007         10/23/2002
4       10/25/2002         06/22/2001
5       06/06/1998         05/05/1995


The query for just finding latest of B is as followed
Code:

SELECT A, MAX(B),C
FROM TB
   GROUP BY A
WITH UR;


I want to extend it to above requirement.

Kindly help ..
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 17, 2009 12:11 pm
Reply with quote

what vsn of db2 are you using?
Back to top
View user's profile Send private message
ashishsr123

New User


Joined: 06 May 2008
Posts: 33
Location: Chennai

PostPosted: Wed Jun 17, 2009 12:26 pm
Reply with quote

DB2
Version 8 Release 1
Back to top
View user's profile Send private message
Poonam Jain

New User


Joined: 06 Apr 2009
Posts: 5
Location: mumbai

PostPosted: Wed Jun 17, 2009 4:53 pm
Reply with quote

Hi,

Try below query............

SELECT X.A,X.B,MAX(Y.C)
FROM
(
SELECT A, MAX(B) AS B
FROM TABLE
GROUP BY A) X INNER JOIN TABLE Y
ON X.B= Y.B
GROUP BY X.A,X.B
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 INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts VB to FB - Finding LRECL SYNCSORT 4
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top