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 > Return Single Row for highest value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-09, 11:52
Howardw Howardw is offline
Registered User
 
Join Date: Nov 2009
Posts: 18
Return Single Row for highest value

DB2 9.5 LUW

I have a table with multiple entries for a user - i want to return a row for each user with latest date.

Example
user - 1000
Rows on table
user - 1000
start date - 2009-05-09
grade - 5
user - 1000
start date - 2009-04-01
grade - 4
user - 1000
start date - 2009-02-01
grade -3

I used max on the date but it did not work - nor did distinct

Help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 12-16-09, 12:04
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by Howardw View Post

I used max on the date but it did not work - nor did distinct
MAX() works for me just fine; if it did not for you - you must have used it incorrectly.

I hope my answer is as specific as your question.
Reply With Quote
  #3 (permalink)  
Old 12-16-09, 12:14
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Howard,
It is typically helpful to give what SQL you have tried, so we can show you what needs to be corrected to fit your needs. If it is a complex question then table layout and some sample data would, also, help.
As Nick states the MAX() function works perfectly in all cases, so there is obviously something wrong with the SQL that you wrote or the data involved.
Something like the following should do what you are after:
select user, max(date)
from table....
where...
group by user

Dave
Reply With Quote
  #4 (permalink)  
Old 12-16-09, 12:48
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb t1.start_date = max(t2.start_date)

Quote:
Originally Posted by Howardw View Post
DB2 9.5 LUW

I have a table with multiple entries for a user - i want to return a row for each user with latest date.

Example
user - 1000
Rows on table
user - 1000
start date - 2009-05-09
grade - 5
user - 1000
start date - 2009-04-01
grade - 4
user - 1000
start date - 2009-02-01
grade -3

I used max on the date but it did not work - nor did distinct

Help would be appreciated.
Code:
select t1.* from table1 t1
where start_date = (select max(t2.start_date) from table1 t2
                               where t2.user = t1.user )
What is easier then DB2 ?

Lenny
Reply With Quote
  #5 (permalink)  
Old 12-16-09, 12:58
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
If you want a row for a specific user with latest date,
you can use "ORDER BY start_date DESC" and "FETCH FIRST 1 ROW ONLY".

Please see "Retrieving MAXimum row"
in "SQL on Fire! Part 1":
http://www.sirdug.org/downloads/SQLonFire_1_SirDUG.pdf
Reply With Quote
  #6 (permalink)  
Old 12-16-09, 13:01
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Nothing is new under Moon !

Lenny
Reply With Quote
  #7 (permalink)  
Old 12-16-09, 13:13
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Updated: Added AS q for DB2 9.1 for LUW or earlier.

Sometimes, using OLAP specification is more efficient than using correlated scalar-fullselect(or self join) with MAX() function.
Code:
SELECT user , start_date , grade
  FROM (SELECT t.*
             , ROW_NUMBER()
                  OVER( PARTITION BY user
                            ORDER BY start_date DESC ) rn
          FROM table_sample t
       ) AS q
 WHERE rn = 1
;

Last edited by tonkuma; 12-16-09 at 13:21.
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