WHERE 절 IN 또는 EXISTS 활용법 문의 드립니다.
SELECT 'SLC017, SLC030, SLC035' ROOT_B FROM DB_ROOT; ROOT_B 컬럼의 데이터를 아래 조건처럼 구현하고 있는데요.
'SLC017, SLC030, SLC035' ROOT_B의 데이터 값은 'SLC017, SLC030, SLC035' 세개 일수도 있고 'SLC017, SLC030' 두개 일수도 있고, 'SLC017' 한개 일수도 있습니다.
IN이나 EXISTST를 이용해 컬럼값과 COUNT값을 같이 아래 처럼 추출하려고 하는데요. 현재 구문으로는 추출이 되질 않아 문의드립니다...
ROOT_B | CNT |
SLC023 | 3 |
SLC008 | 2 |
SLC013 | 3 |
SELECT ROOT_A, COUNT(*)
FROM
(SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC008' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC008' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC015' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC015' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC016' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC016' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT)
WHERE
ROOT_A IN(SELECT 'SLC017, SLC030, SLC035' ROOT_B FROM DB_ROOT)
GROUP BY ROOT_A;
SELECT ROOT_A, COUNT(*)
FROM
(SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC023' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC008' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC008' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC013' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC015' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC015' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC016' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC016' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC012' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC017' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC030' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT
UNION all
SELECT 'SLC035' ROOT_A FROM DB_ROOT)
WHERE
EXISTS(SELECT 'SLC017, SLC030, SLC035' ROOT_B FROM DB_ROOT b WHERE ROOT_A = b.ROOT_B)
GROUP BY ROOT_A;
-- CUBRID -- WITH t AS ( SELECT 'SLC023' root_a FROM db_root UNION ALL SELECT 'SLC023' FROM db_root UNION ALL SELECT 'SLC023' FROM db_root UNION ALL SELECT 'SLC008' FROM db_root UNION ALL SELECT 'SLC008' FROM db_root UNION ALL SELECT 'SLC013' FROM db_root UNION ALL SELECT 'SLC013' FROM db_root UNION ALL SELECT 'SLC013' FROM db_root UNION ALL SELECT 'SLC015' FROM db_root UNION ALL SELECT 'SLC015' FROM db_root UNION ALL SELECT 'SLC016' FROM db_root UNION ALL SELECT 'SLC016' FROM db_root UNION ALL SELECT 'SLC012' FROM db_root UNION ALL SELECT 'SLC012' FROM db_root UNION ALL SELECT 'SLC012' FROM db_root UNION ALL SELECT 'SLC017' FROM db_root UNION ALL SELECT 'SLC017' FROM db_root UNION ALL SELECT 'SLC017' FROM db_root UNION ALL SELECT 'SLC017' FROM db_root UNION ALL SELECT 'SLC030' FROM db_root UNION ALL SELECT 'SLC030' FROM db_root UNION ALL SELECT 'SLC030' FROM db_root UNION ALL SELECT 'SLC035' FROM db_root UNION ALL SELECT 'SLC035' FROM db_root UNION ALL SELECT 'SLC035' FROM db_root ) SELECT root_a , COUNT(*) cnt FROM t WHERE INSTR('SLC017, SLC030, SLC035', root_a) > 0 GROUP BY root_a ;