by 손님 [SQL Query] 조인 function 데이터 표현 [2013.03.29 15:52:52]
-- 함수를 하나 만들어 놓으면 사용(개발)하긴 편하지만 성능이 떨어질 수 있습니다. -- 코드의 종류가 많지 않다면 스칼라서브쿼리가 성능에 좋습니다. -- 같은 코드로 조회시 이전 조회시 캐시된 결과를 재사용합니다. -- 1. 스칼라서브쿼리 : 무식해 보일수도잇지만, 성능 비교 해봐야 함 -- SELECT mainseq , SUBSTR((SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode1) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode2) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode3) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode4) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode5) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode6) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode7) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode8) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode9) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode10) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode11) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode12) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode13) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode14) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode15) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode16) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode17) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode18) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode19) || (SELECT '/'||servicename FROM servicecode WHERE servicecode = m.servicecode20) , 2) AS totalserviceName , A컬럼, B컬럼 FROM mainTable m ; -- 2. 조인, Group By SELECT m.mainseq , REPLACE(wm_concat(s.servicename), ',', '/') totalserviceName , m.A컬럼, m.B컬럼 FROM mainTable m , servicecode s WHERE s.servicecode IN ( m.servicecode1 , m.servicecode2 , m.servicecode3 , m.servicecode4 , m.servicecode5 , m.servicecode6 , m.servicecode7 , m.servicecode8 , m.servicecode9 , m.servicecode10 , m.servicecode11, m.servicecode12, m.servicecode13, m.servicecode14, m.servicecode15 , m.servicecode16, m.servicecode17, m.servicecode18, m.servicecode19, m.servicecode20 ) GROUP BY m.mainseq, m.A컬럼, m.B컬럼 ; -- 3. 짬뽕 방식. 스칼라서브쿼리 캐시효과 없을 듯. SELECT m.mainseq , (SELECT REPLACE(wm_concat(s.servicename), ',', '/') FROM servicecode s WHERE s.servicecode IN ( m.servicecode1 , m.servicecode2 , m.servicecode3 , m.servicecode4 , m.servicecode5 , m.servicecode6 , m.servicecode7 , m.servicecode8 , m.servicecode9 , m.servicecode10 , m.servicecode11, m.servicecode12, m.servicecode13, m.servicecode14, m.servicecode15 , m.servicecode16, m.servicecode17, m.servicecode18, m.servicecode19, m.servicecode20 ) ) totalserviceName , A컬럼, B컬럼 FROM mainTable m ;