select a.loca_name, a.loca_id, ifnull(b.surv_id, 0) from location as a left outer join( select loca_id, count(surv_id) as surv_id from survey_his group by loca_id ) as b on (a.loca_id = b.loca_id) group by a.loca_id
요런 SQLite 쿼리를 오라클로 바꾸려고 하는데 뭘 해도 잘 안 되네요 ㅠㅠ...
테이블도 약간 다르고 한데
select l.loca_name, l.loca_id, count(h.surv_no) from location l, surveyH h where l.loca_id = h.surv_loca_id group by l.loca_id
이렇게 쓰면 그룹 바이 표현식이 아니라고 뜹니다...ㅠㅠ
결과값은
"기타부서" "1" "0"
"강남지원" "2" "6"
"경영지원팀" "3" "2"
"지원사무실" "4" "582"
"사업지원팀" "5" "0"
"시험분석팀" "6" "0"
"개성검사센터" "7" "0"
"제품검사팀" "8" "0"
이런 식으로 나와야 하는데 어떻게 바꿔야 할까요 ...
-- 1. ANSI 표준 유지 -- SELECT a.loca_name , a.loca_id , NVL(b.surv_id, 0) cnt -- IFNULL > NVL FROM location a -- AS 제거 LEFT OUTER JOIN (SELECT loca_id , COUNT(surv_id) AS surv_id FROM survey_his GROUP BY loca_id ) b -- AS 제거 ON (a.loca_id = b.loca_id) -- GROUP BY a.loca_id -- 불필요한 부분 ORDER BY a.loca_id -- 정렬 필요 ; -- 2. 서브쿼리(인라인뷰) 제거 -- SELECT a.loca_name , a.loca_id , COUNT(b.surv_id) cnt FROM location a LEFT OUTER JOIN survey_his b ON a.loca_id = b.loca_id GROUP BY a.loca_id, a.loca_name ORDER BY a.loca_id ; -- 3. Oracle 아우터 조인 SELECT a.loca_name , a.loca_id , COUNT(b.surv_id) cnt FROM location a , survey_his b WHERE a.loca_id = b.loca_id(+) -- 아우터조인(+) GROUP BY a.loca_id, a.loca_name ORDER BY a.loca_id ;