안녕하세요 LAG 함수 질문 드립니다.
WITH T AS ( SELECT 1 SEQ, 'A' ST FROM DUAL UNION ALL SELECT 2 SEQ, 'B' ST FROM DUAL UNION ALL SELECT 3 SEQ, NULL ST FROM DUAL UNION ALL SELECT 4 SEQ, NULL ST FROM DUAL UNION ALL SELECT 5 SEQ, 'C' ST FROM DUAL UNION ALL SELECT 6 SEQ, 'D' ST FROM DUAL UNION ALL SELECT 7 SEQ, 'E' ST FROM DUAL UNION ALL SELECT 8 SEQ, 'F' ST FROM DUAL UNION ALL SELECT 9 SEQ, 'G' ST FROM DUAL UNION ALL SELECT 10 SEQ, 'H' ST FROM DUAL UNION ALL SELECT 11 SEQ, NULL ST FROM DUAL UNION ALL SELECT 12 SEQ, NULL ST FROM DUAL UNION ALL SELECT 13 SEQ, NULL ST FROM DUAL UNION ALL SELECT 14 SEQ, 'F' ST FROM DUAL ) SELECT T.*, DECODE( T.ST, NULL, LAG(T.ST) OVER(ORDER BY SEQ ASC), T. ST) IF_NULL_LAG FROM T
위와 같이 현재 null 값일 경우에는 상위 데이터, 상위도 null이면 상상위....... 로 등록하려고 합니다.
현재 출력정보(검정색) 에 빈값이 없이 모두 출력하려고 하는데(붉은색까지...)
SEQ | ST | IF_NULL_LAG |
1 | A | A |
2 | B | B |
3 | B | |
4 | B | |
5 | C | C |
6 | D | D |
7 | E | E |
8 | F | F |
9 | G | G |
10 | H | H |
11 | H | |
12 | H | |
13 | H | |
14 | F | F |
lag 함수에 몇번째 상위인지를 지정할 수 있지만, 현재값위로 몇번째인지 카운팅 되어야 하는데 잡힐듯 말듯 하네요..
혹시 좋은 방법이 있을까요?
감사합니다.
WITH T AS ( SELECT 1 SEQ, 'A' ST FROM DUAL UNION ALL SELECT 2 SEQ, 'B' ST FROM DUAL UNION ALL SELECT 3 SEQ, NULL ST FROM DUAL UNION ALL SELECT 4 SEQ, NULL ST FROM DUAL UNION ALL SELECT 5 SEQ, 'C' ST FROM DUAL UNION ALL SELECT 6 SEQ, 'D' ST FROM DUAL UNION ALL SELECT 7 SEQ, 'E' ST FROM DUAL UNION ALL SELECT 8 SEQ, 'F' ST FROM DUAL UNION ALL SELECT 9 SEQ, 'G' ST FROM DUAL UNION ALL SELECT 10 SEQ, 'H' ST FROM DUAL UNION ALL SELECT 11 SEQ, NULL ST FROM DUAL UNION ALL SELECT 12 SEQ, NULL ST FROM DUAL UNION ALL SELECT 13 SEQ, NULL ST FROM DUAL UNION ALL SELECT 14 SEQ, 'F' ST FROM DUAL ) SELECT A.ST , FIRST_VALUE(A.ST) OVER(PARTITION BY A.GB ORDER BY A.SEQ ASC) VAL FROM ( SELECT T.* , SUM(DECODE(ST,NULL,0,1)) OVER(ORDER BY T.SEQ ) GB FROM T ) A