# SPL: grouping by enumeration criteria

When grouping, we often need to assign records that meet the same criterion to the same group. For example, divide cities into large, medium and small ones according to the urban population, sort families into rich, middle class, well-off, and poor ones according to annual income, and so forth. According to the pre-defined enumeration criteria, the qualified members are assigned to the corresponding group, which is referred to as the enumeration grouping.

## 1. Saving each member in the first matching group

The members of the set to be grouped are taken as parameters to calculate the criteria according to the order of the enumeration criterion set defined in advance.

[e.g. 1] According to the city population of Chinese cities, classify and calculate the cities. Cities with more than 2 million people are megacity behemoths, cities with 1-2 million people are megacities, cities with 0.5-1 million people are big cities, and others are medium-sized and small cities. Some of the data are as follows:

The A.enum() function in SPL is used for enumeration grouping, and by default each member is saved only in the first matching group.

The SPL script looks like this:

A1: import the city population table.

A2: define grouping criteria.

A3: use the A.enum() function to enumerate and group city population tables by criteria, and by default save each member in the first matching group.

A4: define the name of each group.

A5: calculate the number of cities in each group based on the results of enumerated grouping.

## 2. Saving mismatching members in a new group

When grouping by enumeration criteria, there may be members that do not match any of the criteria, and we can save them in a new group. This applies to scenarios where we care not only about the information of matching members, but also about other mismatching members.

[e.g. 2] To calculate the average salaries by group, the employees are divided into three groups by age: those under 35, those under 45, and others. The data of the employee table are as follows:

option @n of the A.enum() function in SPL is used to save mismatching members to a new group when grouping by enumeration.

The SPL script is as follows:

A1: import the employee table.

A2: define grouping criteria.

A3: use the A.enum() function to enumerate groups by age, use the option @n to save the mismatching members to a new group.

A4: define the name of each group.

A5: calculate the average salary per employee based on the results of enumerated grouping.

## 3. Overlapped grouping according to enumeration criteria

Sometimes we need to redistribute members that meet multiple grouping criteria to all corresponding groups.

[e.g.3] According to the city GDP table, calculate the per capita GDP of the municipalities, the first-tier cities and the second-tier cities respectively. It should be noted that the subgroups may have overlapped members, for example, Beijing is both a first-tier city and a municipality. Some of the data are as follows:

The option @r of the A.enum() function in SPL is used to query whether all the members match or not for each group when enumerating the groups.

The SPL script looks like this:

A1: import the city GDP table.

A2: define municipalities, first- and second-tier city constant variables.

A3: enumerate the criteria of municipalities, first-tier cities and second-tier cities.

A4: use the A.enum() function to group by enumeration criteria, and use the option @r to query whether all members of each group match or not.

A5: define the name of each group.

A6: calculate the per capita GDP for each group based on the results of enumerated grouping.