7.11 Association on interval: write correspondence in a formula
We have the following data:
| Quantity | Price |
|---|---|
| 221 | |
| 87 | |
| 33 | |
| 73 | |
| 162 | |
| 227 | |
| 403 | |
| 288 | |
| 78 | |
| 213 | |
| 374 | |
| 152 |
Calculate the Price value according to the Quantity value. The rule is that different quantity intervals correspond to different prices, as the following shows:
| Quantity | Price |
|---|---|
| 30-50 | 15 |
| 50-100 | 13.75 |
| 100-300 | 13 |
| 300-500 | 12.5 |
SPL script:
| A | |
|---|---|
| 1 | =T(“data.xlsx”) |
| 2 | =A1.run(Price=[15,13.75,13,12.5]([30,50,100,300,500].pseg@r(Quantity))) |
A2 Use pseg function to find ordinal number of the segment that contains the Quantity value in the interval [30,50,100,300,500], and get the corresponding price from the sequence of prices [15,13.75,13,12.5] and assign it to Price field.
Execution result:
| Quantity | Price |
|---|---|
| 221 | 13 |
| 87 | 13.75 |
| 33 | 15 |
| 73 | 13.75 |
| 162 | 13 |
| 227 | 13 |
| 403 | 12.5 |
| 288 | 13 |
| 78 | 13.75 |
| 213 | 13 |
| 374 | 12.5 |
| 152 | 13 |
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/sxd59A8F2W
Youtube 👉 https://www.youtube.com/@esProc_SPL