Oracle - RANK Dates to select last for each day

pm-archtect used Ask the Experts™

I need to find only the last record for each date/time for each unitid in the table.

For each unitid there are many records all submitted over a timespan during the day (every day).  Only the last submission on a given date/time for each unitid is required.

If you look at the attached sample, it shows the starting point and the desired result.

The desired result is one row for each unitid for each day - and that row is the last date/time that an entry was submitted.

I think I need to rank these by date in decending order where the rank < 2 but I'm not sure how to create the window over unitid and date.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2012
try this...
Most Valuable Expert 2011
Top Expert 2012

note, you could use RANK or DENSE_RANK instead of ROW_NUMBER, but since you only need one,  the extra sorting/numbering features of those don't really apply
Geert GOracle dba
Top Expert 2009

don't you need to partition by day ? (the every day part of the Q text)

select * from
(select t.*,row_number() over(partition by unitid, to_char('yyyymmdd', datereceived) order by to_char('yyyymmdd', datereceived) desc, datereceived desc) r
from yourtable)
where r = 1


Thank you for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial