I am trying to find the most recent record based on a date field. When I set latest = 1 in the where clause, I get an error. Please help if possible. DATE is a the field I’m sorting by. I have tried both latest = 1 and latest = ‘1’
SELECT
STAFF_ID,
SITE_ID,
PAY_LEVEL,
ROW_NUMBER() OVER (PARTITION BY STAFF_ID ORDER BY DATE DESC) latest
FROM OWNER.TABLE
WHERE END_ENROLLMENT_DATE is null
AND latest = 1
you can’t use aliases from select list inside the WHERE clause (because of the Order of Evaluation of a SELECT statement)
also you cannot use
OVERclause inside WHERE clause – “You can specify analytic functions with this clause in the select list or ORDER BY clause.” (citation from docs.oracle.com)