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
 

 

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: 6967
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 finding delayed job/s in OPC jzhardy IBM Tools 0 Mon May 29, 2017 9:04 am
No new posts Sorting of hex values Saurabh_mi DFSORT/ICETOOL 11 Thu May 25, 2017 3:49 pm
No new posts Replace values in the input data Vikas Maharnawar DFSORT/ICETOOL 10 Thu May 11, 2017 2:18 pm
No new posts Compare two files and subtract values ameetmund DFSORT/ICETOOL 7 Fri Mar 31, 2017 3:35 pm
No new posts Check Binary Values for symbols gandikk CA Products 3 Tue Mar 21, 2017 5:11 am


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