select max date from joined tables with columns with unique values

Hi I am at my wits end!

I want to get the most recent date from joined tables with unique values in 2 columns. How do I do this? I have also tried the ranking (but John has the same rank) and tried rownum = 1, but I still get the same results below for some reason
  1. Name     ID            Email                  DeptNum      DeptScore      OnDate
  2. John     A46       john@doe.com            100                  50                  5/11/2011
  3. John     A46       johndoe@aol.com      200                  75                   7/21/2015
  4. Alice    B33       alice@hotmail.com      100                  50                  4/15/2014

I want to get the following:
  1. Name     ID            Email                  DeptNum      DeptScore      OnDate
  2. John     A46       johndoe@aol.com      200                  75                   7/21/2015
  3. Alice    B33       alice@hotmail.com      100                  50                  4/15/2014

My query (oracle)
select distinct e.name, e.id, e.email, d.deptnum, d.deptscore, d.ondate
from  emp e
inner join dept d on d.deptnum = e.dnum
    and d.ondate = e.livedate
    and d.ondate = (select max(m.ondate) from dept m where d.ondate = m.ondate)
--where e.id in ('A46','B33')

Open in new window


Thanks!
Ramanth LexAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
How about this?

SELECT name,
       id,
       email,
       deptnum,
       deptscore,
       ondate
  FROM (SELECT e.name,
               e.id,
               e.email,
               d.deptnum,
               d.deptscore,
               d.ondate,
               ROW_NUMBER() OVER(PARTITION BY name, id ORDER BY ondate DESC) rn
          FROM emp e INNER JOIN dept d ON d.deptnum = e.dnum AND d.ondate = e.livedate)
 WHERE rn = 1

Open in new window


If it doesn't produce the desired results, please post data for emp and dept (each table, not the results of a join) that produce wrong results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ramanth LexAuthor Commented:
Thank you, good sir!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.