Difference between two unordered deliminted lists (Oracle)
Is there a simple elegant method to return the difference between开发者_StackOverflow中文版 two unordered, delimited lists in Oracle?
Example:
- List A: a1, b4, g3, h6, t8, a0
- List B: b4, h6, a0, t8, a1
Difference: g3
If you have access to APEX_UTIL
, you can parse the strings into an array, convert them to collections, then use MULTISET EXCEPT
(which is the same as MINUS but for collections):
SET SERVEROUT ON
DECLARE
TYPE set_t IS TABLE OF varchar2(100);
listA APEX_APPLICATION_GLOBAL.vc_arr2;
listB APEX_APPLICATION_GLOBAL.vc_arr2;
excpt set_t;
FUNCTION to_set_t (arr IN APEX_APPLICATION_GLOBAL.vc_arr2)
RETURN set_t IS
rset set_t := set_t();
BEGIN
rset.EXTEND(arr.COUNT);
FOR i IN 1..arr.COUNT LOOP
rset(i) := TRIM(arr(i));
END LOOP;
RETURN rset;
END;
BEGIN
-- parse lists into arrays
listA := APEX_UTIL.string_to_table('a1, b4, g3, h6, t8, a0',',');
listB := APEX_UTIL.string_to_table('b4, h6, a0, t8, a1',',');
-- convert arrays to collections, then do the minus
excpt := to_set_t(listA) MULTISET EXCEPT to_set_t(listB);
-- display the results
FOR i IN 1..excpt.COUNT LOOP
DBMS_OUTPUT.put_line(excpt(i));
END LOOP;
END;
Result:
g3
More info on the MULTISET operators, which were introduced in 10g: http://www.oracle-developer.net/display.php?id=303
精彩评论