Link to home
Start Free TrialLog in
Avatar of pm-archtect
pm-archtectFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Oracle - RANK Dates to select last for each day


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.

Avatar of Sean Stuber
Sean Stuber

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sean Stuber
Sean Stuber

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
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
Avatar of pm-archtect


Thank you for your help.