만일 A->A-1 -> A-2 ->A-3으로 ID가 변경된 사람의 모든 이력을 보고 싶다면 아래처럼 쿼리를 하시면 될 것 같구요.
WITH T AS (
SELECT 1 SEQ,'A' ID,'ADDR' COL_NAME,'인천1' PREV_VAL,'인천2' CURR_VAL,'20100101' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 2 SEQ,'A' ID,'TELNO' COL_NAME,'111' PREV_VAL,'222' CURR_VAL,'20100101' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 3 SEQ,'A' ID,'TEL_NO' COL_NAME,'222' PREV_VAL,'333' CURR_VAL,'20100101' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 4 SEQ,'A' ID,'PWD' COL_NAME,'123QWE' PREV_VAL,'QWE123' CURR_VAL,'20100201' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 5 SEQ,'A' ID,'ADDR' COL_NAME,'인천2' PREV_VAL,'서울1' CURR_VAL,'20100203' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 6 SEQ,'A' ID,'ADDR' COL_NAME,'서울1' PREV_VAL,'서울2' CURR_VAL,'20100311' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 7 SEQ,'A' ID,'TEL_NO' COL_NAME,'333' PREV_VAL,'444' CURR_VAL,'20100401' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 8 SEQ,'A' ID,'ID' COL_NAME,'A' PREV_VAL,'A-1' CURR_VAL,'20100402' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 9 SEQ,'A-1' ID,'ADDR' COL_NAME,'서울2' PREV_VAL,'서울3' CURR_VAL,'20100405' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 10 SEQ,'A-1' ID,'TELNO' COL_NAME,'444' PREV_VAL,'555' CURR_VAL,'20100411' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 11 SEQ,'A-1' ID,'TELNO' COL_NAME,'555' PREV_VAL,'666' CURR_VAL,'20100421' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 12 SEQ,'A-1' ID,'TELNO' COL_NAME,'666' PREV_VAL,'777' CURR_VAL,'20100511' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 13 SEQ,'A-1' ID,'ID' COL_NAME,'A-1' PREV_VAL,'A-2' CURR_VAL,'20100512' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 14 SEQ,'A-2' ID,'ID' COL_NAME,'A-2' PREV_VAL,'A-3' CURR_VAL,'20100515' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 15 SEQ,'A-3' ID,'PWD' COL_NAME,'QWE123' PREV_VAL,'ASD123' CURR_VAL,'20100521' REG_DATE,'ADMIN' REG_ID FROM DUAL UNION ALL
SELECT 16 SEQ,'A-3' ID,'ADDR' COL_NAME,'서울3' PREV_VAL,'부천1' CURR_VAL,'20100601' REG_DATE,'ADMIN' REG_ID FROM DUAL
)
SELECT *
FROM T
WHERE ID IN(
SELECT ID
FROM T
START WITH ID='A-3'
CONNECT BY PRIOR ID=CURR_VAL
AND COL_NAME='ID'
)
--AND COL_NAME='ADDR' --주소변경 이력만 보고 싶을때
ORDER BY REG_DATE DESC
;
언제 이력테이블에 데이타를 입력할 것인가의 문제는 마농님 말씀처럼 트리거를 이용하셔도 되고 프로그램단에서 특정 4개 칼럼이 변경될 때마다 INSERT 하는 메소드를 호출해도 될 것 같습니다.
by lovely랑
[2010.07.02 13:58:29]
아 저렇게 하면 되는군요.
전 재귀적으로 설계 할려구 생각중이었는데 , 님의 말씀이 더 좋은 방법 같습니다.
조언 정말 감사드립니다.
^^
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.