5.9 Iteration: The early termination cumulation
Perform iteration operation by loop, where both the computing expression and termination condition are user-defined.
Find on which date when the sales target of 150,000 of the first quarter is achieved in the year 2014 based on the sales table. Below is part of the table:
OrderID | Customer | SellerId | 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 |
… | … | … | … | … |
SPL script:
A | |
---|---|
1 | =connect(“db”).query@x(“select * from sales”) |
2 | =A1.select(year(OrderDate)==2014) |
3 | =A2.iterate((@+=Amount, ~~=OrderDate),0,@>150000) |
A1 Connect to the data source and import the sales table.
A2 Select records of the year 2014.
A3 Use iterate() function to perform an iteration operation, where the initial value is 0, to sum up sales amounts cumulative to the current record until the total reaches or exceeds the sales target, and returns the order date of the eligible record.
Execution result:
Value |
---|
2014/03/25 |
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
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