Pengaturan Oracle :
CREATE TABLE table_name ( ID, A, B ) AS
SELECT 1, 'a', 'a1' FROM DUAL UNION ALL
SELECT 2, 'b', 'b1' FROM DUAL UNION ALL
SELECT 3, 'a', 'a2' FROM DUAL UNION ALL
SELECT 4, 'c', 'a1' FROM DUAL UNION ALL
SELECT 5, 'b', 'b2' FROM DUAL;
Kueri - Gunakan GROUP BY
dan COUNT( DISTINCT ... )
:
SELECT A
FROM table_name
WHERE B IN ( 'a1', 'a2' ) -- The items in the list
GROUP BY A
HAVING COUNT( DISTINCT b ) = 2; -- The number of items in the list
Keluaran :
A
-
a
Kueri - Melewati daftar secara dinamis :
CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(10);
/
SELECT A
FROM table_name
WHERE B MEMBER OF :your_list
GROUP BY A
HAVING COUNT( DISTINCT B ) = CARDINALITY( :your_list );
Di mana variabel ikat :your_list
bertipe stringlist
.
Jika daftar dilewatkan sebagai string yang dibatasi maka Anda dapat menggunakan salah satu teknik di Memisahkan string yang dibatasi halaman dokumentasi untuk memisahkannya. Ada fungsi PL/SQL sederhana yang akan mengembalikannya sebagai koleksi yang dapat dicolokkan ke kueri di atas.
Perbarui :
SELECT A
FROM table_name
GROUP BY A
HAVING COUNT( DISTINCT CASE WHEN b IN ( 'a1', 'a2' ) THEN b END ) = 2
AND COUNT( DISTINCT CASE WHEN b NOT IN ( 'a1', 'a2' ) THEN b END ) = 0;
atau
SELECT A
FROM table_name
GROUP BY A
HAVING COUNT( DISTINCT CASE WHEN b MEMBER OF :your_list THEN b END ) = CARDINALITY( :your_list )
AND COUNT( DISTINCT CASE WHEN b NOT MEMBER OF :your_list THEN b END ) = 0;