10.11 Get equal time intervals
Divide a time interval between two dates into N equal segments.
Divide sale records dated from 2014/01/20 to 2015/01/20 (not inclusive) into four groups according to date, and store them to different files. Below is part of the sale data:
ORDERID | CUSTOMERID | EMPLOYEEID | ORDERDATE | AMOUNT |
---|---|---|---|---|
10400 | EASTC | 1 | 2014/01/01 | 3063.0 |
10401 | HANAR | 1 | 2014/01/01 | 3868.6 |
10402 | ERNSH | 8 | 2014/01/02 | 2713.5 |
10403 | ERNSH | 4 | 2014/01/03 | 1005.9 |
10404 | MAGAA | 2 | 2014/01/03 | 1675.0 |
… | … | … | … | … |
The range(s,e,k:n) function divide records between s and e into n equal segments and return the first record of segment k and that of segment segment k+1. The result is a 2-tuple sequence.
SPL script:
A | B | |
---|---|---|
1 | =file(“Sales.txt”).import@qt() | |
2 | [2014/01/20,2015/01/20] | |
3 | for 4 | =range(A2(1),A2(2), A3:4) |
4 | =A1.select(B3(1)<=OrderDate && OrderDate<B3(2)) | |
5 | =file(“Sales”+string(B3(1), “yyyyMMdd”)+“.txt”).export@t(B4) |
A1 Import Sales table.
A2 Define the start date and the end date.
A3-B3 Perform a loop operation, where range() function divides the date interval into four parts and return the A3th part each time.
B4 Get records through matching dates to corresponding subintervals.
B5 Create a file and export selected records in B4 to it.
Execution result:
Members |
---|
2014/01/20 |
2014/04/22 |
Members |
---|
2014/04/22 |
2014/07/22 |
Members |
---|
2014/07/22 |
2014/10/21 |
Members |
---|
2014/10/21 |
2015/01/20 |
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL