DOCUMENT_ID JOB_ID PERSON_ID
1 2 18
1 3 16
2 4 12
2 5 27
DOCUMENT_ID JOB_ID PERSON_ID
1 3 16
2 5 27
SELECT
DISTINCT document_id, MAX(job_id) OVER (PARTITION BY document_id)
FROM my_table;
WITH
cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY document_id ORDER BY job_id DESC) rn
FROM my_table
)
SELECT document_id, job_id, person_id
FROM cte
WHERE rn = 1;
SELECT DISTINCT
document_id,
MAX(job_id) OVER (PARTITION BY document_id) job_id,
FIRST_VALUE(person_id) OVER (PARTITION BY document_id ORDER BY job_id DESC) person_id
FROM my_table