Вот пример, который может дать вам идею.Посмотрите.
SQL> with
2 -- a long list of (duplicate?) IDs
3 all_ids (col) as
4 (select '538693 , 538693 , 541616 , 541616 , 541616 , 541620 , 541116 , 541116 , 538639 , 538639' from dual
5 ),
6 -- that's what you get from your query
7 do_query (col) as
8 (select '541116 , 538639 , 538639' from dual
9 ),
10 -- split ALL_IDs to rows
11 all_1 as
12 (select regexp_substr(col, '[^ , ]+', 1, level) col
13 from all_ids
14 connect by level <= regexp_count(col, ',') + 1
15 ),
16 -- split IDs from DO_QUERY to rows as well
17 do_1 as
18 (select regexp_substr(col, '[^ , ]+', 1, level) col
19 from do_query
20 connect by level <= regexp_count(col, ',') + 1
21 ),
22 -- MINUS set operator will return IDs from ALL_IDS that aren't in DO_QUERY IDs
23 minus_me as
24 (select col From all_1
25 minus
26 select col from do_1
27 )
28 -- finally, compose the remaining **unique** IDs back
29 select listagg(col, ' , ') within group (order by col) result
30 from minus_me;
RESULT
--------------------------------------------------------------------------------
538693 , 541616 , 541620
SQL>