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