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
 
Need help in query , finding max values

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Need help in query , finding max values
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: 6970
Location: porcelain throne

PostPosted: Wed Jun 17, 2009 12:11 pm    Post subject:
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    Post subject:
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    Post subject: Reply to: Need help in query , finding max values
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    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 query to fetch record which has only ... maxsubrat DB2 12 Mon Dec 11, 2017 5:03 pm
No new posts Finding the plan when code compiled u... Ahemad DB2 1 Fri Dec 01, 2017 6:38 pm
No new posts How to query MAXGENS of an existing P... Chuchulo Hatyzak JCL & VSAM 4 Wed Nov 22, 2017 10:23 pm
No new posts Native SQL Query kishpra DB2 1 Wed Nov 22, 2017 8:38 pm
No new posts Query on IEFBR14 with GDG Ashishpanpaliya JCL & VSAM 4 Tue Nov 07, 2017 8:34 pm

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