With Tab AS (
SELECT sysdate dt, 0 col1, 0 col2, 0 col3 from dual union all
SELECT sysdate, 1 , 0, 0 from dual union all
SELECT sysdate, 0 ,1, 0 from dual union all
SELECT sysdate, 0 , 0, 0 from dual union all
)
일 때
col1, co2, col3 이 모두 0인 Row 만 가져오는 쿼리가 어떻게 될까요?
단순히
select * from tab
where col1 =0 and col2=0 and col3=0 ;
이렇게 말구요. ^^
지금은 Column을 세개만 있는데 컬럼 수가 많은 경우 할 수 있는 쿼리가 궁금합니다.^^.
WITH Tab (pkey, dt, col1, col2, col3, col4, col5) AS ( SELECT 1, sysdate, 0 , 0, 0, 0, 0 FROM dual UNION ALL SELECT 2, sysdate, 1 , 0, 0, 1, 0 FROM dual UNION ALL SELECT 3, sysdate, 0 , 1, 0, 0, 0 FROM dual UNION ALL SELECT 4, sysdate, 0 , 0, 0, 0, 0 FROM dual ) SELECT tab.* FROM tab JOIN ( SELECT pkey FROM tab UNPIVOT ( tvalue FOR tname IN ( col1, col2, col3, col4, col5 ) ) WHERE tvalue = 0 -- 동일해야 하는 값 AND tname in ( UPPER('col1'), UPPER('col2'), UPPER('col3'), UPPER('col4'), UPPER('col5') ) -- 동일해야 하는 칼럼들 GROUP BY pkey HAVING COUNT(pkey) = 5 -- 동일해야 하는 칼럼수 ) BB ON tab.pkey = BB.pkey