WITH T AS ( SELECT 'AAA' AS v1, 'KAO' AS v2 FROM DUAL UNION ALL SELECT 'AAA', 'KBO' FROM DUAL UNION ALL SELECT 'AAA', 'KAO' FROM DUAL UNION ALL SELECT 'BBB', 'KBO' FROM DUAL UNION ALL SELECT 'CCC', 'KAT' FROM DUAL ) SELECT v1, v2 FROM ( SELECT T.*, CASE WHEN ( COUNT(*) OVER(PARTITION BY v1) = SUM(DECODE(SUBSTR(v2, 1, 2), 'KA', 1, 0)) OVER(PARTITION BY v1) ) THEN 'true' ELSE 'false' END AS only_ka FROM T ) WHERE only_ka = 'true'
WITH T (COL1 , COL2 )AS ( SELECT 'AAA', 'KAOITD' FROM DUAL UNION ALL SELECT 'AAA', 'KAIDJA' FROM DUAL UNION ALL SELECT 'AAA', 'KBWSAZ' FROM DUAL UNION ALL SELECT 'AAA', 'KBRWAD' FROM DUAL UNION ALL SELECT 'BBB', 'KAOITD' FROM DUAL UNION ALL SELECT 'BBB', 'KAIDJA' FROM DUAL UNION ALL SELECT 'BBB', 'KBWSAZ' FROM DUAL UNION ALL SELECT 'BBB', 'KBRWAD' FROM DUAL UNION ALL SELECT 'CCC', 'KAOITD' FROM DUAL UNION ALL SELECT 'CCC', 'KAIDJA' FROM DUAL ) SELECT COL1 , COL2 FROM T WHERE COL1 = 'CCC' AND REGEXP_LIKE ( COL2 , '^KA' )