12. Intra-group operations and rankings
11. Cumulative count of continuous values
meteorolog.txt stores meteorological data. Based on daily average temperature (TEMP), compute the difference between daily BEIJING temperature and the daily average temperature of all observation stations, and find ranking of temperature at BEIJING station among temperatures of all stations (in descending order).
Expected result:
SPL code:
A | |
---|---|
1 | =T(“meteorolog.txt”,STATION,DATE,TEMP) |
2 | =A1.group(DATE; round(~.avg(TEMP),3):AVGTEMP, ~.(TEMP):TEMPRecs) |
3 | =A1.select(STATION:“BEIJING”) |
4 | =join@p(A3:bj;A2:all) |
5 | =A4.new(bj.DATE:DATE, bj.TEMP:TEMP,all.AVGTEMP:AVGTEMP, round(bj.TEMP-all.AVGTEMP,3):TEMPDiff, all.TEMPRecs.rank@z(bj.TEMP):TEMPRank) |
A1 retrieves certain fields from the data file. A2 prepares the daily total data needed by the target computation. It groups records by date, and computes daily average temperature (AVGTEMP) and gets all temperature data in each day. Below is A2’s result set:
Only data at BEIJING station will be computed during the computation, so the other data is not needed. A3 selects orgirinal records of BEIJING station from A1:
Compare tables in A2 and A3 and we find that both are ordered by date. A2 contains the daily summary data of all stations. A3 has daily records of one station. To compute temperature difference and the ranking, the two tables need to be joined. So in A4, join() function joins them up. Since both of them are arranged by date, the function uses @p option to match records one by one according to row numbers. Here is A4’s result set:
bj represents observation data at BEJING station. all represents the daily summary data of all stations. A5 generates the result table based on the joining result set, during which fields of the corresponding table can be referenced as needed. all.TEMPRecs.rank@z(bj.TEMP), for example, computes the ranking of temperature at BEIJING station based on temperature data of all stations. Here @z option is used to arrange rankings in descending order.
13. Text analysis using regular expression
Contents and Exercise Data
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/2bkGwqTj
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version