The DIFFERENCE row function takes two sets of the same data type and returns a set containing all of the elements of the first set that do not appear in the second set.
DIFFERENCE(<set1>, <set2>)where:
DIFFERENCE({ 1, 2, 3, 4, 5 }, { 1, 3, 5 }) = { 2, 4 }
DIFFERENCE({ }, { 1, 3, 5 }) = { }
DIFFERENCE({ 1, 2, 3 }, { }) = { 1, 2, 3 }
DIFFERENCE({ 1, 2 }, { 'a', 'b' }) yields a checking error because the two sets are not of the same data type
Record 5: Body=Earthy, Silky, Tannins
Flavors=Blackberry, Earthy, Toast
Record 6: Body=Robust
Flavors=Berry, Plum, Zesty
Record 7: Body=Silky, Tannins
Flavors=Cherry, Pepper, Prune
Record 8: Body=Oak, Robust
Flavors=Cherry, Oak, Raspberry
Record 9: Body=Fruit, Strawberry, Silky, Tannins
Flavors=Fruit, Earthy, Strawberry
RETURN results AS SELECT WineID AS idRec, DIFFERENCE(Body, Flavors) AS diffAttrs WHERE WineID BETWEEN 5 AND 9 ORDER BY idRec
diffAttrs idRec
----------------------------
| { Silky, Tannins } | 5 |
| { Robust } | 6 |
| { Silky, Tannins } | 7 |
| { Robust } | 8 |
| { Silky, Tannins } | 9 |
----------------------------
Records 5, 7, and 9 have "Silky" and "Tannins" in the Body set, but these values do not appear in the Flavors set. Likewise, Records 6 and 8 have "Robust" in the Body set, but that value does not appear in the Flavors set.
RETURN results AS SELECT WineID AS idRec, DIFFERENCE(Flavors, Body) AS diffAttrs WHERE WineID BETWEEN 5 AND 9 ORDER BY idRec
diffAttrs idRec
-----------------------------------
| { Blackberry, Toast } | 5 |
| { Berry, Plum, Zesty } | 6 |
| { Cherry, Pepper, Prune } | 7 |
| { Cherry, Raspberry } | 8 |
| { Earthy } | 9 |
-----------------------------------
To take Record 9 as an example of the output, "Earthy" is the only element from the first set (the Flavors set) that does not appear in the second set (the Body set).