칼럼명은 제가 임의로 수정한 부분이 있어요
HQ_CODE를 HQ_CD로
테이블명을 AA,BB로 .. 수정해서 실행해 보세요 ^^
SELECT HQ_CD,
DECODE(A,1,
(SELECT MAX(b.BASIC_SEQ) FROM BB b
WHERE b.ANSWER_GRADE = 'Y' AND b.HQ_CD = a.HQ_CD),
(SELECT MAX(b.BASIC_SEQ) FROM BB b
WHERE b.ANSWER_GRADE = 'N' AND b.HQ_CD = a.HQ_CD)) B
FROM (
SELECT HQ_CD,
SIGN(SUM(DECODE(ANSWER_GRADE,'Y',1,0))) A
FROM AA
WHERE ANSWER_GRADE IS NOT NULL
GROUP BY HQ_CD
) A;