select hos_code ,joen1 from (select A.hos_code ,case when B.TYPE_CODE = '90601000' then B.SPE_NUM end joen1 from TB_GENERAL A left join TB_MED B on A.YYYY = B.YYYY and A.hos_code = B.HOS_CODE where A.yyyy = '2016' and A.TYPE_CODE = B.TYPE_CODE and A.hos_code = '11100028' -- GROUP BY A.hos_code ) a -- group by hos_code
쿼리를 실행시키면 첨부파일의 결과가 나옵니다.
jeon1의 경우 값이 1개만 있고 나머지는 전부 null이라 group by로 값이 있는경우 하나만 불러오려고 했는데
아래 에러가 생깁니다. 어떻게 수정해야 값을 제대로 불러올지 답변좀 부탁드립니다.
Column 'TB_MED.TYPE_CODE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
인라인뷰도 필요 없어 보이고,
아우터 조인도 필요 없어 보이는데요?
조건을 Case 가 아닌 Where 절에서 주면 되는 것 아닌가요?
SELECT a.hos_code , b.spe_num AS joen1 FROM tb_general a INNER JOIN tb_med b ON a.yyyy = b.yyyy AND a.hos_code = b.hos_code AND a.type_code = b.type_code WHERE a.yyyy = '2016' AND a.hos_code = '11100028' AND a.type_code = '90601000' ;