If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Finding max value within another column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-21-09, 21:18
RonSMeyer RonSMeyer is offline
Registered User
 
Join Date: Dec 2008
Posts: 5
Question Finding max value within another column

It seems like this should be a simple task but for some reason I'm stumped. I've tried various joins, select where IN plus some others. I can't get what I want.

I have a table that contains account-number decimal (9) and tran-date date(4) both non-unique. For each account-number I want to return one and only one row that contains the account-number and the most recent tran-date ie. the Max(tran-date) for EACH individual account-number. I don't care about the other data in the table I just want these two columns which I will then use in a join with a different table.

Any thoughts on how to do this?
Reply With Quote
  #2 (permalink)  
Old 05-21-09, 22:21
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
This seems a little too simplistic to be causing you problems but..
Code:
SELECT account_number, MAX(tran-date)
FROM table-name
GROUP BY account_number
This is will return distinct Account_numbers and the Max tran-date for each account.

Sample data:
Code:
1	2009-01-01
1	2009-01-01
1	2009-02-02
1	2009-03-03
2	2009-01-01
2	2009-03-03
2	2009-04-04
2	2009-04-04
Result:
Code:
1 2009-03-03
2 2009-04-04
Reply With Quote
  #3 (permalink)  
Old 05-22-09, 09:20
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
The other way, which to my huge surprise a couple of years ago, is faster in some cases:
SELECT account_number, tran-date
FROM table-name A
where ....
and tran_date = (SELECT MAX(B.tran-date)
FROM table-name B
where A.account_number = B.account_number
)
Dave
Reply With Quote
  #4 (permalink)  
Old 05-23-09, 23:35
db2dummy1 db2dummy1 is offline
Registered User
 
Join Date: Feb 2009
Posts: 114
I would go with method #3 ... anyone?
Reply With Quote
  #5 (permalink)  
Old 05-24-09, 11:00
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Do you need to use DDL for method #3?
Reply With Quote
  #6 (permalink)  
Old 05-24-09, 12:23
db2dummy1 db2dummy1 is offline
Registered User
 
Join Date: Feb 2009
Posts: 114
Not sure. Just waiting to see how many creative solutions we can come up with for a kindergarden level SQL question
Reply With Quote
  #7 (permalink)  
Old 05-24-09, 12:56
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
xaxaxa, db2dummy
Reply With Quote
  #8 (permalink)  
Old 05-25-09, 00:07
RonSMeyer RonSMeyer is offline
Registered User
 
Join Date: Dec 2008
Posts: 5
Thanks to those who answered.

I apologize to the rest of you for insulting you with such a kindergarten question.

I won't ask any more questions on this "helpful" forum.
Reply With Quote
  #9 (permalink)  
Old 05-25-09, 07:44
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
RonSMeyer, I wouldn't let a few posters who only seem to supply remarks and not help stop you from using this site (or others). Most of the people are helpful and a lot of good information can be found.

I, personally, don't find any question a problem no matter what the level as long as the person is trying to learn.

I would point you to Graeme Birchall's site where you can download a free "DB2 SQL Cookbook' to help you learn SQL. It is located here:

DB2 SQL Cookbook

PS As strange as it is to me now, GROUP BY was the hardest concept I came across when learning SQL. We had a very specialized (and limited) use for DB2 and there was never any application that it could be applied to (or anyone to ask with more experience). This was either before or just at the start of the Internet expansion to the general public. I would have very much liked a site like this to ask my beginner SQL questions.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On