From 088e66508633db5b656e98e65f449c2340e95f9d Mon Sep 17 00:00:00 2001 From: nidhibhammar <59045594+nidhibhammar@users.noreply.github.com> Date: Mon, 21 Aug 2023 19:49:02 +0530 Subject: [PATCH] Initial draft of Multiset Intersect and Multiset Except as per DB-2315 --- .../02_using_the_multiset_union_operator.mdx | 144 +++++++++++++++++- 1 file changed, 140 insertions(+), 4 deletions(-) diff --git a/product_docs/docs/epas/16/application_programming/epas_compat_spl/12_working_with_collections/02_using_the_multiset_union_operator.mdx b/product_docs/docs/epas/16/application_programming/epas_compat_spl/12_working_with_collections/02_using_the_multiset_union_operator.mdx index 9629d65c761..32ab5f62579 100644 --- a/product_docs/docs/epas/16/application_programming/epas_compat_spl/12_working_with_collections/02_using_the_multiset_union_operator.mdx +++ b/product_docs/docs/epas/16/application_programming/epas_compat_spl/12_working_with_collections/02_using_the_multiset_union_operator.mdx @@ -9,7 +9,7 @@ redirects: The `MULTISET UNION` operator combines two collections to form a third collection. The signature is: ```sql - MULTISET UNION [ ALL | DISTINCT | UNIQUE ] + MULTISET [ UNION | INTERSECT | EXCEPT ] [ ALL | DISTINCT | UNIQUE ] ``` Where `coll_1` and `coll_2` specify the names of the collections to combine. @@ -20,6 +20,8 @@ Include the `DISTINCT` or `UNIQUE` keyword to include duplicate elements in the ## Combining collections +### MULTISET UNION + This example uses the `MULTISET UNION` operator to combine `collection_1` and `collection_2` into a third collection, `collection_3`: ```sql @@ -75,11 +77,8 @@ END; COUNT: 4 Results: 10 20 30 40 ``` - The resulting collection includes only those members with distinct values. -## Removing duplicate entries - In this example, the `MULTISET UNION DISTINCT` operator removes duplicate entries that are stored in the same collection: ```sql @@ -107,3 +106,140 @@ END; COUNT: 5 Results: 10 20 30 40 50 ``` + +### MULTISET INTERSECT + +`MULTISET INTERSECT` takes as arguments two nested tables and returns a nested table whose values are common in the two input nested tables. The two input nested tables must be the same type, and the returned nested table must be of the same type as well. + +This example uses the `MULTISET INTERSECT` operator to combine `color_name` and `fruit_name` into a third collection, `common_name`: + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Orange', 'Peach', 'Yellow', 'Peach'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach', 'Peach'); + common_name := color_name MULTISET INTERSECT UNIQUE fruit_name; + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Orange +Peach +``` + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Orange', 'Peach', 'Yellow', 'Peach'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach', 'Peach'); + common_name := color_name MULTISET INTERSECT DISTINCT fruit_name; + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Orange +Peach +``` + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Orange', 'Peach', 'Yellow', 'Peach'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach', 'Peach'); + common_name := color_name MULTISET INTERSECT ALL fruit_name; + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Orange +Peach +Peach +``` + +### MULTISET EXCEPT + +`MULTISET EXCEPT` takes two nested tables as arguments and returns a nested table whose elements are in the first nested table but not in the second nested table. The two input nested tables must be the same type, and the returned nested table must be of the same type as well. + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Blue', 'Orange', 'Peach', 'Yellow'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach'); + common_name := color_name MULTISET EXCEPT UNIQUE fruit_name; + + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Blue +Green +Red +Yellow +``` + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Blue', 'Orange', 'Peach', 'Yellow'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach'); + common_name := color_name MULTISET EXCEPT DISTINCT fruit_name; + + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Blue +Green +Red +Yellow +``` + +```sql +DECLARE + TYPE name_typ IS TABLE OF VARCHAR(50); + color_name name_typ; + fruit_name name_typ; + common_name name_typ; +BEGIN + color_name := name_typ('Red', 'Green', 'Blue', 'Blue', 'Orange', 'Peach', 'Yellow'); + fruit_name := name_typ('Mango', 'Orange', 'Grapes', 'Banana', 'Peach'); + common_name := color_name MULTISET EXCEPT ALL fruit_name; + + FOR i IN common_name.FIRST .. common_name.LAST LOOP + DBMS_OUTPUT.PUT_LINE(common_name(i)); + END LOOP; +END; +__OUTPUT__ +Red +Green +Blue +Blue +Yellow +``` + +