Skip to content

Commit

Permalink
Initial draft of Multiset Intersect and Multiset Except as per DB-2315
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhibhammar committed Aug 22, 2023
1 parent 199c667 commit 088e665
Showing 1 changed file with 140 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ redirects:
The `MULTISET UNION` operator combines two collections to form a third collection. The signature is:

```sql
<coll_1> MULTISET UNION [ ALL | DISTINCT | UNIQUE ] <coll_2>
<coll_1> MULTISET [ UNION | INTERSECT | EXCEPT ] [ ALL | DISTINCT | UNIQUE ] <coll_2>
```

Where `coll_1` and `coll_2` specify the names of the collections to combine.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
```


0 comments on commit 088e665

Please sign in to comment.