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 > retrieving max(date) and corresponding value in a query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-10, 13:34
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
retrieving max(date) and corresponding value in a query

Hi,

I have a table with these columns ---


ID READINGVALUE DATEADDED
----------- ------------------------ --------------------------
4328 5 2007-09-21-19.33.36.558139
4328 6 2007-10-24-17.25.32.255380
4328 7 2008-01-08-19.43.58.038657


I want to retrieve the readingvalue corresponding to MAX(DATEDADDED). Is this the most efficient way to retrieve it --

db2 "select * from table1 where id=4328 and DATEADDED=( SELECT MAX(DATEADDED) FROM table1 where id=4328)"

ID READINGVALUE DATEADDED
----------- ------------------------ --------------------------
4328 7 2008-01-08-19.43.58.038657
Reply With Quote
  #2 (permalink)  
Old 12-21-10, 15:18
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Linear Maximum

Most effective this one, which I am using for 3 years, already.

I called this operation as "Linear Maximum":

Code:
SELECT 
      INT(SUBSTR(MAX(CHAR(DATEADDED) || CHAR(READINGVALUE)),27))
from table1 where id=4328
Lenny
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