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 > SQL - recent row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-03-04, 05:04
Tank Tank is offline
Registered User
 
Join Date: Feb 2004
Location: Copenhagen
Posts: 220
Question SQL - recent row

Hi All (AIX 5.2 DB2 8.1.4)

I have to select 10 variables from a table. 6 of these variables, together
make up a unique identifier for a row, the sixth variable is a date.
Often the other 5 variables will be identical across rows.

Now my question:

How do I select all unique rows on from the table,
but only the rows with the recentmost date, if the rows are duplicates
(on the 5 variables) ?

All thougths are appreciated.
__________________
Kristian K. Hansen
Project Supervisor
National Board of Health
Reply With Quote
  #2 (permalink)  
Old 05-03-04, 07:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
something like this, perhaps--
Code:
select pkcol1
     , pkcol2
     , pkcol3
     , pkcol4
     , pkcol5
     , datecol
  from yourtable ZZ
 where datecol =
       ( select max(datecol)
           from yourtable
          where pkcol1 = ZZ.pkcol1   
            and pkcol2 = ZZ.pkcol2   
            and pkcol3 = ZZ.pkcol3   
            and pkcol4 = ZZ.pkcol4   
            and pkcol5 = ZZ.pkcol5
       )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-03-04, 08:12
Tank Tank is offline
Registered User
 
Join Date: Feb 2004
Location: Copenhagen
Posts: 220
Talking Great!!

Awesome - and simple solution

Thanks
__________________
Kristian K. Hansen
Project Supervisor
National Board of Health
Reply With Quote
  #4 (permalink)  
Old 05-03-04, 08:26
aloz aloz is offline
Registered User
 
Join Date: May 2003
Location: San Juan, PR
Posts: 18
Most recent value

You can try this:

SELECT tbl1.col1, tbl1.col2, tbl1.col3, tbl1.col4, tbl1.col5, MAX(tbl1.date1)
FROM tbl1
GROUP BY tbl1.col1, tbl1.col2, tbl1col3, tbl1.col4, tbl1.col5;
Reply With Quote
  #5 (permalink)  
Old 05-03-04, 09:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
aloz, that works only if col1 through col5 are the only columns you want to return in the row along with the max date

my solution allows you to add additional columns to the SELECT list

you cannot just add columns to your SELECT list, because then they'd also have to go into the GROUP BY, and since they would not be part of the primary key, the rows you get back could then duplicate the primary key columns
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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