5.1 Reference the previous/next row

 

It is not unusual that we need to reference a value in another row when computing data arranged in a certain order. One instance is to calculate the growth rate of the price in each day compared with the previous day.
SPL allows users to reference a neighboring row through the relative position during a loop operation.
Here the task is to find growth rate of each day’s closing price compared with the previous day according to the following SSE Composite Index data:

DATE CLOSE OPEN VOLUME AMOUNT
2020/01/02 3085.1976 3066.3357 292470208 3.27197122606E11
2020/01/03 3083.7858 3089.022 261496667 2.89991708382E11
2020/01/06 3083.4083 3070.9088 312575842 3.31182549906E11
2020/01/07 3104.8015 3085.4882 276583111 2.88159227657E11
2020/01/08 3066.8925 3094.2389 297872553 3.06517394459E11

SPL script:

A
1 =T(“SSEC.csv”)
2 =A1.sort(DATE)
3 =A2.derive(CLOSE/CLOSE[-1]:INCREASE)

A1: Import the SSE Composite Index data.
A2: Sort A1’s data by DATE.
A3: Calculate the growth rate of each day’s closing price compared with the previous day, where CLOSE[-1] is price of the previous day. derive() function adds a computed column to the table sequence.