Non-basic Aggregation Examples

Non-basic Aggregation Examples

1. SUM aggregate after enumeration grouping

【Example 1】 Based on the GDP table, calculate the GDP per capita for direct-controlled municipalities, first-tier cities and second-tier cities respectively. Below is part of the source table:

IDCityGDPPopulation
1Shanghai326792418
2Beijing303202171
3Shenzhen246911253
4Guangzhou230001450
5Chongqing203633372

【SPL script】

AB
1=connect("db")/ Connect to database
2=A1.query("select * from GDP")/ Query GDP table
3[["Beijing","Shanghai","Tianjin","Chongqing"].pos(?)>0,
["Beijing","Shanghai","Guangzhou","Shenzhen"].pos(?)>0,
["Chengdu","Hangzhou","Chongqing","Wuhan","Xian","Suzhou",
"Tianjin","Nanjing","Changsha","Zhengzhou","Dongguan","Qingdao",
"Shenyang","Ningbo","Kunming"].pos(?)>0]
/ Enumerate direct-controlled cities, first-tier cities and second-tier cities respectively
4=A2.enum@r(A3,City)/ Group records in GDP table according to the enumerated sequences of cities
5=A4.new(A3(#):Area,~.sum(GDP)/~.sum(Population)*10000:CapitaGDP)/ Calculate GDP per capita in each group, during which sum() function is used to calculate sum

A5’s result:

AreaCapitaGDP
["Beijing","Shanghai","Tianjin","Chongqing"].pos(?)>0107345.03
["Beijing","Shanghai","Guangzhou","Shenzhen"].pos(?)>0151796.49
["Chengdu","Hangzhou","Chongqing","Wuhan","Xi’an","Suzhou","Tianjin","Nanjing",
"Changsha","Zhengzhou","Dongguan","Qingdao","Shenyang","Ningbo","Kunming"].pos(?)>0
106040.57

2. Merge overlapping time intervals

【Example 2】 The following is part of the orders table. We want to merge records of customer ANATR that have overlapping time periods (between order date and finish date).

OrderIDCustomerSellerIdOrderDateFinishDate
10308ANATR72012/09/182012/10/16
10309ANATR32012/09/192012/10/17
10625ANATR32013/08/082013/09/05
10702ANATR12013/10/132013/11/24
10759ANATR32013/11/282013/12/26

【SPL script】

AB
1=connect("db")/ Connect to data source
2=A1.query("select * from Orders where Customer='ANATR'order by OrderDate")/ Get orders records of customer ANATR and sort them by order date
3=A2.group@i(OrderDate>max(FinishDate[,-1]))/ Group the selected orders records, during which a new group is created when the order date of the current record is later than all previous finish dates
4=A3.new(Customer,~.min(OrderDate):OrderDate,~.max(FinishDate):FinishDate)/ For each group, min()function gets the earliest order date and max() function gets the latest finish date, and use them respectively as the new order date and the new finish date

A4’s result:

CustomerOrderDateFinishDate
ANATR2012/09/182012/10/17
ANATR2013/08/082013/09/05
ANATR2013/10/132013/11/24
ANATR2013/11/282013/12/29

3. Grouping & conditional COUNT aggregate

【Example 4】 The following is part of the scores table. We want to, for each subject, find the number of students in class one who fail in this subject.

CLASSSTUDENTIDSUBJECTSCORE
Class one1English84
Class one1Math77
Class one1PE69
Class one2English81
Class one2Math80

【SPL script】

AB
1=connect("db")/ Connect to database
2=A1.query("select * from Scores where CLASS='Class one'")/ Get scores records of students in class one
3=A2.groups(SUBJECT; count(SCORE<60):FailCount)/ Perform grouping & aggregation, during which count() function calculates the number of students who fail in the subject

A3’s result:

SUBJECTFailCount
English2
Math0
PE2

4. Perform logical AND on a set of Boolean values

【Example 5】 Based on the following primary school online-learning terminal tables (as shown by pic 2), we want to find if all students use mobile phones to learn. Pic 1 is the directory where the tables for classes of all grades are stored.

undefined
IDSTUDENT_NAMETERMINAL
1Rebecca MoorePhone
2Ashley WilsonPhone,PC,Pad
3Rachel JohnsonPhone,PC,Pad
4Emily SmithPhone,Pad
5Ashley SmithPhone,PC
6Matthew JohnsonPhone
7Alexis SmithPhone,PC
8Megan WilsonPhone,PC,Pad

【SPL script】

ABC
1=directory@ps("D:/Primary School")/ Traverse the target directory recursively to list all files
2for A1=file(A2).xlsimport@t()/ Import the Excel files of all classes circularly
3=B2.([TERMINAL,"Phone"].ifn().split@c().pos("Phone") > 0)|@/ ifn() function makes sure that a null terminal value is treated as being able to use phone and returns True
4=B3.cand()/ A.cand() function checks whether B3’s members are all True

A4’s result:

Value
false

5. Perform logical OR on a set of Boolean values

【Example 6】 The following is part of the sales data. We want to find if there is at least one month in 2014 when customer RATTC’s amount ranks in top 3.

OrderIDCustomerSellerIdOrderDateAmount
10400EASTC12014/01/013063.0
10401HANAR12014/01/013868.6
10402ERNSH82014/01/022713.5
10403ERNSH42014/01/031005.9
10404MAGAA22014/01/031675.0

【SPL script】

AB
1=connect("db").query("select * from sales")/ Connect to the data source to query sales table
2=A1.select(year(OrderDate)==2014)/ Get records of 2014
3=A2.group(month(OrderDate))/ Group records of 2014 by months
4=A3.(~.groups(Customer; sum(Amount):Amount))/ Group records in each group by customers and calculate each customer’s total amount
5=A4.new(~.top(-3; Amount):Top3)/ Loop through records of each month to get customers whose total amounts rank in top 3
6=A5.(Top3.(Customer).pos("RATTC")>0)/ Among each month’s top 3, check whether customer RATTC is included
7=A6.cor()/ A.cor() function checks if there is a True in A6’s members

A7’s result:

Value
false

Find more examples in SPL CookBook.

Leave a Reply