hopefully someone has an idea for a more performant Oracle SQL.
Following Oracle PERSON-Table:
ID | PERSON_ID | REFERENCE_ID | COMMENT | CREATION_DATE |
---|---|---|---|---|
1 | 10066 | 666 | comment 2 days ago | 01-11-2022 09:16:00.00000 |
2 | 10066 | 111 | single comment | 01-11-2022 11:44:00.00000 |
3 | 10066 | 666 | comment 1 day ago | 02-11-2022 07:37:00.00000 |
4 | 33444 | 666 | comment of different person | 02-11-2022 09:54:00.00000 |
5 | 10066 | 666 | comment today | 03-11-2022 08:46:00.00000 |
6 | 10066 | 987 | another comment | 03-11-2022 09:02:00.00000 |
7 | 10066 | 987 | another comment same day | 03-11-2022 09:44:22.123456 |
I want to have only the recent timestamps of identical REFERENCE_ID results from a specific PERSON_ID.
So I expect 3 rows should be in the result list for PERSON_ID 10066:
PERSON_ID | REFERENCE_ID | COMMENT | CREATION_DATE |
---|---|---|---|
10066 | 111 | single comment | 01-11-2022 11:44:00.00000 |
10066 | 666 | comment today | 03-11-2022 08:46:00.00000 |
10066 | 987 | another comment same day | 03-11-2022 09:44:22.123456 |
I came up with a subselect idea which works, but is possibly not the best / most performant solution:
SELECT * FROM PERSON_TABLE p WHERE CREATION_DATE = (
SELECT MAX(CREATION_DATE)
FROM PERSON_TABLE
WHERE REFERENCE_ID = p.REFERENCE_ID AND PERSON_ID = 10066
);
Has someone a better idea? Or is my approach ok performance wise? I have the feeling there are more optimized statements / queries possible, maybe without subselect.
Thanks in advance, have a nice day!