Oracle Query – How to Retrieve Missing Values of an ID from a List

oraclequerysubquery

have a data structure that is value/tag inside a table on Oracle sql
Lets say I have this structore

exTable

ID TAG VALUE
1 1 x
1 4 y
2 2 x
2 3 y
2 4 z

I want to retrieve the missing tags for a list of values I have, for example [1,2,3,4,5]

In this case Im searching for this as an output if I look for (1,2)

ID MISSINGTAG
1 2
1 3
1 5
2 1
2 5

Been trying with temp tables and joins but im still missing something because when i try to join I either dont get anything or just get the ones that do exists, I achieved want I want for one id using a minus

select column_value as COL_NAME
from table(sys.dbms_debug_vc2coll('1',
'2',
'3',
'4',
'5'))
MINUS
SELECT TAG FROM exTable WHERE ID='1';

But for doing it for a list of IDS im have not had luck

If im duplicating the question let me know

Best Answer

One way is the create the cartesian product between your tags and existing id:s. From there you can subtract your existing table either by MINUS as in your example, or by EXISTS:

with d (tag, id) as (
    select tag, id 
    from table(sys.dbms_debug_vc2coll('1', '2', '3', '4', '5')) t 
    cross join ( 
        select distinct id from extable 
    )
) 
select * from d
where not exists (
    select 1 from extable x where x.id = d.id and x.tag = d.tag
);