WITH V_COP AS ( SELECT 'A01' AS ORG_CD, 300 AS GOAL, '1' AS SEQ FROM DUAL UNION ALL SELECT 'A01' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL SELECT 'A01' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL SELECT 'C05' AS ORG_CD, 600 AS GOAL, '1' AS SEQ FROM DUAL UNION ALL SELECT 'C05' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL SELECT 'C05' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL ) SELECT A.ORG_CD, B.GOAL FROM ( SELECT * FROM V_COP ) A , ( SELECT * FROM V_COP WHERE GOAL IS NOT NULL ) B WHERE A.ORG_CD = B.ORG_CD ;
sql을 보면 ORGA_CD, GOAL, SEQ 컬럼 3개가 존재 할때
ORGA_CD는 모든 레코드에 존재 하지만 GOAL 은 하나만 존재 합니다.
이때 GOAL이 NULL 또는 SEQ=0 인 경우에는 [같은 ORGA_CD] 이면서 [SEQ=1]인 레코드
에서 GOAL을 복사하고 싶습니다.
UNION 이나, JOIN으로 안하고.. 혹시 함수가 있을까 해서요.
위 SQL문장은 JOIN을 이용한 SQL입니다.
그래서 JOIN을 하지 않고, [함수]를 이용해서 위 결과처럼 나오게 하고 싶은 거죠.
부탁 합니다.
-- 약간 조건만 추가했습니다.
WITH V_COP AS
( SELECT 'A01' AS ORG_CD, 300 AS GOAL, '1' AS SEQ FROM DUAL UNION ALL
SELECT 'A01' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL
SELECT 'A01' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL
SELECT 'C05' AS ORG_CD, 600 AS GOAL, '1' AS SEQ FROM DUAL UNION ALL
SELECT 'C05' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL UNION ALL
SELECT 'C05' AS ORG_CD, NULL AS GOAL, '0' AS SEQ FROM DUAL
)
SELECT org_cd, CASE WHEN seq = 0 AND goal IS NULL THEN
MAX(goal) OVER(PARTITION BY org_cd)
ELSE
goal
END AS goal
FROM V_COP