Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select latest record if duplicate oracle

I have a table save sending & receiving process as example

id sender receiver
1 A A
2 A B
3 A C
4 B D
5 D B
6 B D

But i don’t know how to write oracle query to select the latest with unique receiver. Expected result as:

id sender receiver
1 A A
3 A C
5 D B
6 B D

Any ideas on how I can accomplish the result i’m after?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use ROW_NUMBER() here:

WITH cte AS (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY receiver ORDER BY id DESC) rn
    FROM yourTable t
)

SELECT id, sender, receiver
FROM cte
WHERE rn = 1
ORDER BY id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading