sourcetip

Oracle 가장 최근 날짜 레코드 선택

fileupload 2023. 10. 5. 23:29
반응형

Oracle 가장 최근 날짜 레코드 선택

날짜 필드를 기준으로 가장 최근 기록을 찾으려고 합니다.where 절에 latest = 1을 설정하면 오류가 발생합니다.가능하다면 도와주시기 바랍니다.DATE는 제가 분류하고 있는 분야입니다.최신 = 1과 최신 = '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

WHERE 절 내의 select 목록에서 별칭을 사용할 수 없습니다(SELECT 문의 평가 순서 때문에)

또한 사용할 수 없습니다.OVERWHERE 절 내부의 절 - "select list or ORDER BY 절에서 이 절을 사용하여 분석 함수를 지정할 수 있습니다." (docs.oracle.com 에서 citation)

select *
from (select
  staff_id, site_id, pay_level, date, 
  max(date) over (partition by staff_id) max_date
  from owner.table
  where end_enrollment_date is null
)
where date = max_date

staff_id + date를 uk로 가정하면, 이것은 또 다른 방법입니다.

SELECT STAFF_ID, SITE_ID, PAY_LEVEL
  FROM TABLE t
  WHERE END_ENROLLMENT_DATE is null
    AND DATE = (SELECT MAX(DATE)
                  FROM TABLE
                  WHERE staff_id = t.staff_id
                    AND DATE <= SYSDATE)

MAX와 이런 걸 시도해 봐야겠어요.

SELECT staff_id, max( date ) from owner.table group by staff_id

다른 열에 링크를 넣습니다.

select staff_id, site_id, pay_level, latest
from owner.table, 
(   SELECT staff_id, max( date ) latest from owner.table group by staff_id ) m
where m.staff_id = staff_id
and m.latest = date
select *
from (select
  staff_id, site_id, pay_level, date, 
  rank() over (partition by staff_id order by date desc) r
  from owner.table
  where end_enrollment_date is null
)
where r = 1

언급URL : https://stackoverflow.com/questions/11128194/oracle-select-most-recent-date-record

반응형