3.2 Get intersection of two sets
The intersection of set A and set B is made up of members that exist in both set A and set B. For instance, the intersection of [1,2,3] and [1,3,5] is [1,3].
Here is an example. A business has two sales channels – online and offline stores. Their sales data is stored in SALES_ONLINE table and SALES_STORE table respectively. The task is to find out customers who purchase in both online and offline stores in the year 2014. Below is part of data in the two tables:
SPL can use character “^” to calculate intersection of sets. A^B, for instance, calculates intersection of set A and set B.
A1: Import SALES_ONLINE table from the original file and select sales records of the year 2014.
A2: Import SALES_STORE table from the original file and select sales records of the year 2014.
A3: Use A.id() function to get the set of distinct customer IDs from A1 and A2 respectively, and use operator “^” to calculate their intersection, which contains customers who purchase both online and offline.