SQL Query for Max Date

I got this SQL query that I'm running against a table to pick the latest date an item was sold (PurchaseDate), pick the customer that bought it along with other info, the thing is, an item can be returned by one customer and bought by another, so If I'm running Max date function and grouping on Customer, I'll get both customers, the one that returned the item and the one that bought it, and it's critical to bring the customer out in the query. I need to select the item (Unit_Serial_no) by the last date it was sold (PurchaseDate) by that customer, and ignore past customers that had that item as some point. Please advise.
Roberto Madro R.Programmer AnalystAsked:
Who is Participating?

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

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.

awking00Information Technology SpecialistCommented:
select customer, unit_serial_no, model, purchaseDate from
(select customer, unit_serial_no, model, purchaseDate,
 row_number() over (partition by unit_serial_no, model order by purchasedate desc) rn
 from yourtable)
where rn = 1;

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
Roberto Madro R.Programmer AnalystAuthor Commented:
Thank you awking00, just what the doctor ordered.
Roberto Madro R.Programmer AnalystAuthor Commented:
Many thanks
awking00Information Technology SpecialistCommented:
You're welcome - glad to help.
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

From novice to tech pro — start learning today.