6.12 Enumerated grouping: overlapping groups

 

At times we need to put members that meet multiple grouping conditions to all corresponding groups.
Calculate Per Capita GDP in municipalities, first-tier cities and second-tier cities according to city GDP table. Note that one member may belong to more than one group. Beijing, for instance, is both a municipality and a first-tier city. Below is part of data in the city GDP table:

ID CITY GDP POPULATION
1 Shanghai 32679 2418
2 Beijing 30320 2171
3 Shenzhen 24691 1253
4 Guangzhou 23000 1450
5 Chongqing 20363 3372

In SPL, @r option can work with A.enum() function to check whether all members match a condition in each group during an enumerated grouping operation.

SPL script:

A
1 =T(“CityGDP.txt”)
2 [[“Beijing”,“Shanghai”,“Tianjin”,“Chongqing”],[“Beijing”,“Shanghai”,“Guangzhou”,“Shenzhen”],[“Chengdu”,“Hangzhou”,“Chongqing”,“Wuhan”,“Xian”,“Suzhou”,“Tianjin”,“Nanjing”,“Changsha”,“Zhengzhou”,“Dongguan”,“Qingdao”,“Shenyang”,“Ningbo”,“Kunming”]]
3 [A2(1).pos(?)>0,A2(2).pos(?)>0,A2(3).pos(?)>0]
4 =A1.enum@r(A3,CITY)
5 [Municipality,First-tier city, Second-tier city]
6 =A4.new(A5(#):AREA,~.sum(GDP)/~.sum(POPULATION)*10000:CAPITA_GDP)

A1 Import CityGDP table.
A2 Define a constant of municipalities, first-tier cities and second-tier cities.
A3 Enumerate conditions for being a municipality, first-tier city or a second-tier city.
A4 Use A.enum() function to group records according to the enumerated conditions, during which @r option check whether every member matches a condition for the corresponding group.
A5 Define names of groups.
A6 Calculate Per Capita GDP in each group according to result of the enumerated grouping operation.